Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TimePeriod
- {
- //Private seconds field
- private double _seconds;
- //Public Seconds property
- public double Seconds
- {
- get { return _seconds; }
- set { _seconds = value; }
- }
- //Public Minutes property
- public double Minutes
- {
- get { return _seconds / 60; }
- set { _seconds = value * 60; }
- }
- //Public Hours property
- public double Hours
- {
- get { return _seconds / 3600; }
- set { _seconds = value * 3600; }
- }
- }
- //Usage example
- class Program
- {
- static void Main()
- {
- TimePeriod t = new TimePeriod();
- // Assigning the Hours property causes the 'set' accessor to be called.
- t.Hours = 24; //sets the _seconds field
- // Evaluating the Hours property causes the 'get' accessor to be called.
- System.Console.WriteLine("Time in hours: " + t.Hours);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement