A simple object-oriented example

For instance

First, design a class about Time (Time class). The members of this class are as follows:

Code:

//Definition code of Time class
        class Time
        {
            //The following are private members
            private int hour;
            private int minute;
            private int second;

            private void SetTime(int h,int m, int s)
            {
                Hour = h;
                Minute = m;
                Second = s;
            }


            //The following are public members
            //Constructor: no arguments
            public Time()
            {
                SetTime(0, 0, 0);
            }

            //Constructor: 1 parameter
            public Time(int hourValue)
            {
                SetTime(hourValue, 0, 0);
            }

            //Constructor: 3 parameters
            public Time(int hourValue,int minuteValue,int secondValue)
            {
                SetTime(hourValue, minuteValue, secondValue);
            }


            //Property: Hour
            public int Hour
            {
                get { return hour; }

                //If the value of value is legal, assign the value of value to hour, otherwise assign 0 to hour
                set { hour = ((value >= 0 && value < 24) ? value : 0); }
            }

            //Attribute: Minute
            public int Minute
            {
                get { return minute; }
                set { minute = ((value >= 0 && value < 60) ? value : 0); }
            }

            //Property: Second
            public int Second
            {
                get { return second; }
                set { second = ((value >= 0 && value < 60) ? value : 0); }
            }

            //Function: returns the 24-hour time
            public string ToString24()
            {
                string output = Hour + ":" + Minute + ":" + Second;
                return output;
            }

            //Function: returns 12 hour time
            public string ToString12()
            {
                int hourTemp = ((Hour == 12) || (Hour == 0))? 12 : (Hour % 12);
                string AMPM = (Hour <12?"AM":"PM");
                string output = hourTemp + ":" + Minute + ":" + Second + AMPM;
                return output;
            }
        }

In the above example, we use properties to access private variables. The benefits are as follows:

1. The validity of the assignment can be checked.

2. Enhance encapsulation. When the storage form of data changes, only the code in the attribute needs to be changed, and other codes will not be affected.

 

The main program calls the Time class. The code is as follows:

static void Main(string[] args)
        {
            Time time1 = new Time();

            Console.WriteLine("time1:");
            Console.WriteLine("24 Hourly time:" + time1.ToString24()) ;
            Console.WriteLine("12 Hourly time:" + time1.ToString12()+"\n");

            Time time2 = new Time(22);
            Console.WriteLine("time2:");
            Console.WriteLine("24 Hourly time:" + time2.ToString24());
            Console.WriteLine("12 Hourly time:" + time2.ToString12() + "\n");

            Time time3 = new Time(22,36,30);
            Console.WriteLine("time2:");
            Console.WriteLine("24 Hourly time:" + time3.ToString24());
            Console.WriteLine("12 Hourly time:" + time3.ToString12() + "\n");

        }

Operation result:

 

Keywords: Attribute

Added by tave on Wed, 04 Dec 2019 06:32:35 +0200