Properties
2
Objectives
•
Describe properties
–
purpose
–
definition
–
use
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 string name;
}
public field
write
Person p = new Person();
p.name = "Bob";
string n = p.name;
read
4
Private data
•
Class typically has private fields and public access methods
–
clients use methods to access fields
class Person
{
private string name;
public string GetName()
{
return name;
}
public void SetName(string newName)
{
name = newName;
}
}
private field
public read method
public write method
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;
public string GetName()
{
return name;
}
}
supply only read method
for read-only access
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)
{
if (newName == "")
// error
name = newName;
}
}
error checking
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();
p.SetName("Bob");
string n = p.GetName();
read
write
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();
p.SetName("Bob");
string n = p.GetName();
empty parentheses
no assignment operator
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 p = new Person();
p.Name = "Ann";
string n = p.Name;
Person has Name property
write
read
10
Structure of property definition
•
Property definition has
–
declaration of access level, type, and name
–
code block with implementation
class Person
{
public string Name
{
}
}
declaration of
Name property
implementation
11
Properties storage
•
Property does not provide any needed data storage
–
typically define field to store data
class Person
{
private string name;
public string Name
{
}
}
field to store data
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
{
set
{
}
get
{
}
}
}
write method
read method
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
{
name = value;
}
}
}
use value
argument
Person p = new Person();
p.Name = "Ann";
calls set
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 name;
}
}
}
return value
of property
Person p = new Person();
string n = p.Name;
calls get
Không có nhận xét nào:
Đăng nhận xét