属性get set
11570 ワード
、 。 ,
1
NumberOfEmployees, 。
public
class
Employee
{
public
static
int
NumberOfEmployees;
private
static
int
counter;
private
string
name;
//
A read-write instance property:
public
string
Name
{
get
{
return
name; }
set
{ name
=
value; }
}
//
A read-only static property:
public
static
int
Counter
{
get
{
return
counter; }
}
//
A Constructor:
public
Employee()
{
//
Calculate the employee's number:
counter
=
++
counter
+
NumberOfEmployees;
}
}
class
TestEmployee
{
static
void
Main()
{
Employee.NumberOfEmployees
=
100
;
Employee e1
=
new
Employee();
e1.Name
=
"
Claude Vige
"
;
System.Console.WriteLine(
"
Employee number: {0}
"
, Employee.Counter);
System.Console.WriteLine(
"
Employee name: {0}
"
, e1.Name);
}
}
Employee number:
101
Employee name: Claude Vige
。
public
class
Employee
{
private
string
name;
public
string
Name
{
get
{
return
name; }
set
{ name
=
value; }
}
}
public
class
Manager : Employee
{
private
string
name;
//
Notice the use of the new modifier:
public
new
string
Name
{
get
{
return
name; }
set
{ name
=
value
+
"
, Manager
"
; }
}
}
class
TestHiding
{
static
void
Main()
{
Manager m1
=
new
Manager();
//
Derived class property.
m1.Name
=
"
John
"
;
//
Base class property.
((Employee)m1).Name
=
"
Mary
"
;
System.Console.WriteLine(
"
Name in the derived class is: {0}
"
, m1.Name);
System.Console.WriteLine(
"
Name in the base class is: {0}
"
, ((Employee)m1).Name);
}
}
Name
in
the derived
class
is
: John, Manager
Name
in
the
base
class
is
: Mary