Properties

Chia sẻ: Shy Monk | Ngày: | Loại File: PPT | Số trang:20

0
70
lượt xem
3
download

Properties

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

good because it allows clear and simple access syntax – bad because it exposes implementation details of class

Chủ đề:
Lưu

Nội dung Text: Properties

  1. Properties
  2. Objectives • Describe properties – purpose – definition – use 2
  3. Public data • Public data can be both good and bad – good because it allows clear and simple access syntax – bad because it exposes implementation details of class class Person { public field public string name; ... } Person p = new Person(); write p.name = "Bob"; read string n = p.name; 3
  4. Private data • Class typically has private fields and public access methods – clients use methods to access fields class Person { private field private string name; public read method public string GetName() { return name; } public write method public void SetName(string newName) { name = newName; } } 4
  5. Advantage: access control • Private data lets class offer only desired access methods – read-only – write-only – read and write class Person { private string name; supply only read method public string GetName() for read-only access { return name; } } 5
  6. Advantage: data validation • Private data allows class to perform data validation – methods can do error checking – clients can not avoid validation code by accessing data directly class Person { public void SetName(string newName) { error checking if (newName == "") // error ... name = newName; } ... } 6
  7. Disadvantage: access method names • Typical to prefix access method names with Get and Set – follows common wisdom to make method names verb phrases – but conflicts with concept of field as representing data property Person p = new Person(); write p.SetName("Bob"); read string n = p.GetName(); 7
  8. Disadvantage: access method usage • Syntax of access methods can be undesirable – set does not use = so is not obviously a write operation – get typically has awkward looking empty parentheses Person p = new Person(); no assignment operator p.SetName("Bob"); empty parentheses string n = p.GetName(); 8
  9. Properties • Properties combine the advantages of public and private data – have clean access syntax like public fields – provide error checking opportunity like private fields Person has Name property Person p = new Person(); write p.Name = "Ann"; read string n = p.Name; 9
  10. Structure of property definition • Property definition has – declaration of access level, type, and name – code block with implementation class Person { declaration of public string Name Name property { implementation ... } ... } 10
  11. Properties storage • Property does not provide any needed data storage – typically define field to store data class Person { field to store data private string name; public string Name { ... } ... } 11
  12. Property accessors • Property can define set and get accessors – definitions go inside implementation code block – no explicit arguments or return types class Person { public string Name { write method set { ... } read method get { ... } } ... } 12
  13. Set implementation • Set typically records new data for property – new data passed in hidden argument called value class Person { private string name; public string Name { set { use value name = value; argument } ... } ... } Person p = new Person(); calls set p.Name = "Ann"; 13
  14. Get implementation • Get typically returns current value of property – uses return to return value class Person { private string name; public string Name { get { return value return name; of property } ... } ... } Person p = new Person(); ... calls get string n = p.Name; 14
  15. Validation • Property accessors can contain validation code class Person { private string name; public string Name { set { validation if (value == "") // error ... name = value; } ... } ... } 15
  16. Access control • Property may provide any access desired – read-only – write-only – read and write class Person { Age property public int Age { supply get and omit get set for read-only { ... } } ... } 16
  17. Data storage • Property does not need to be backed by matching field – value may be produced in any way that is appropriate class Person { store date private DateTime dob; of birth public int Age { get { calculate age int age = DateTime.Today.Year - dob.Year; DateTime birthday = dob.AddYears(age); birthday occurred if (birthday > DateTime.Today) yet this year? age--; return age; } } ... } 17
  18. Static property • Property can be static – must be accessed through type name class Item { private static int revenue; define static public static int Revenue property { get { return revenue; } set { revenue = value; } } ... } access using Item.Revenue = 5; type name 18
  19. Access level • Property accessors have same access level – both public, both private, etc. class Person { public property public string Name { public read method set { ... } public write method get { ... } } ... } 19
  20. Summary • Properties model traits of a class or object – provide advantages of private field / public accessor pattern – with clean user syntax 20
Đồng bộ tài khoản