cool hit counter C# - set and get_Intefrankly

C# - set and get


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/22647989

In Object-Oriented Programming (OOP), the requirement is to put is not to allow the outside world to directly access the member variables of the class directly, since they can not be accessed, then what is the point of defining these member variables? So C# uses set and get methods to access private member variables, which are equivalent to a channel, an "interface", for the outside world to access the object. Let's start with a piece of code.

 class Employee
    {
        private string name;
        private byte age;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public byte Age
        {
            get { return age; }
            set { age = value; }
        }
    }

The code defines two private variables, name and age, which can be accessed using attributes when we don't want the outside world to have free access to the private variables, with the syntax

    public <Return Type( To be of the same type as the variable being accessed)> < property name( Cannot have the same name as the accessed variable)>
        {
            get{ return < Accessed variables>;}
            set{ < Accessed variables> = value;}
        }

When we use a property to access a private member variable we call the get method inside, and when we want to modify that variable we call the set method, but of course you can define just a get method or just a set method when you define it. If only the get method is defined, then the corresponding variable is "read-only"; if only the set method is defined, then the corresponding variable is "write-only".

When I saw this, I had a question, since the outside world can access private members within the class via set and get, why not just define it as common and make it directly accessible to the outside world? Take the Employee class above and illustrate.

class Employee
    {
        private string name;
        private byte age;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        //**** after modification****↓↓↓↓↓↓↓↓
        public byte Age
        {
            get { return age; }  
            set
            {                
                if (value > 10 && value<=100)   // The age of the employees of the company is generally in the10 up to100 Between the ages of
                age = value;   
            }
        }
        //**** after modification****↑↑↑↑↑↑↑↑
    }

I believe it will be able to be very clear after reading this example. In the example, set is like a gatekeeper uncle and only good people are allowed to enter. This can be done through the attribute control Reading and writing to member variables, preventing illegal assignments to member variables, etc.

Just the other day at the Academic Exchange, we talked about client changes, and they have a non-trivial role to play in this regard, or to give a small example.

There is a class of thermometers.

class Thermometer
    {
        private double temperature;
        public Thermometer(double temperature)  // constructor (computing)
        {
            this.temperature = temperature;
        }
        public double Temperature
        {
            get { return temperature; }
            set { temperature = value; }
        }
    }

Assuming that the temperature units here are expressed in degrees Celsius (°C), if the customer's original intention is misunderstood during the requirements analysis phase or if the customer changes the requirements at a later date, all the variables temperature in the system that express temperature need to be used to express Kelvin temperature (K). Then instead of moving the hundreds of Temperature properties in the system, just change the code slightly in the get and set functions to.

class Thermometer
    {
        private double temperature;
        public Thermometer(double temperature)  // constructor (computing)
        {
            this.temperature = temperature;
        }
        public double Temperature
        {
            //**** after modification****↓↓↓↓↓↓↓↓
            get { return temperature-273.15; }
            set { temperature = value+273.15; }
            //**** after modification****↑↑↑↑↑↑↑↑
        }
    }

Here's a simple console application to test it out, with the main function

 class Test
    {
        static void Main(string[] args)
        {
            Thermometer a = new Thermometer(40);
            Console.WriteLine(a.Temperature);
        }
    }

The initial value given to the temperature using the constructor is 40 degrees, so the result of the run before the code change is

The code is modified to run as follows.

If there are any shortcomings and mistakes, please leave your valuable comments and suggestions, thank you very much!


Recommended>>
1、Ideas and implementation of a batch automatic grading program for writing programming assignments in Python
2、Sad at the end of the month its all about statistical information
3、Resources2019 AI Academic Conference List All Deadlines are Here
4、Personalized Recommendation System VII ABTestab Testing Platform
5、How to build input image pyramids in face detection

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号