Console.WriteLine("Age: {0}", empAge);
Console.WriteLine("Pay: {0}", currPay);
}
}
Now assume you have created an Employee object named joe. On his birthday, you wish to
increment the age by one. Using traditional accessor and mutator methods, you would need to
write code such as the following:
Employee joe = new Employee();
joe.SetAge(joe.GetAge() + 1);
However, if you encapsulate empAge using a property named Age, you are able to simply write
Employee joe = new Employee();
joe.Age++;
C H A P T E R 5
■
D E F I N I N G E N C A P S U L AT E D C L A S S T Y P E S
168
8849CH05.qxd 10/22/07 1:34 PM Page 168
Do'stlaringiz bilan baham: |