Subclass rewrites properties and fields in parent class

Subclass rewrites properties and fields in parent class

At the beginning of listening to the teacher, fields should be modified with private, attributes should be modified with public, fields should be controlled through attributes, and fields should be used to store data.

So why use private to modify fields? ? ?

Reason: Prevent external personnel from rewriting the properties and fields in the parent class through subclasses, thereby destroying the original intention of the project and causing serious consequences.

example:

Parent class and members:

class Bea
    {
        public Bea(int age,string name)
        {
            this.Name = name;
            this.Age = age;
        }
        public  int _age;
         // Rewriting the age attribute 
        public  virtual  int Age
        {
            get { return _age; }
            set
            {
                if (value >= 15 && value <= 20)
                {
                    _age = value;
                }
                else
                {
                    _age = 0;
                }
            }                         
        }
        public string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

Subclasses and members:

1  class Green:Bea
 2      {
 3          public Green( string a, int b): base (b,a)
 4          {
 5          }
 6          // Rewriting the Age property in the parent class 
7          public  override  int Age
 8          {
 9              get { return _age; }
 10              set 
11              {
 12                  if (value >= 18 && value <= 400 )
 13                  {
 14                     _age = value;
15                 }
16                 else
17                 {
18                     _age = 0;
19                 }
20             }
21         }
22     }

Next, output the member Age in the parent class in the main method

1  class Program
 2      {
 3          static  void Main( string [] args)
 4          {            
 5              Bea father = new Green( " danger " , 300 );//Assign the subclass object to the parent class
 6              Console.WriteLine(father.Age) ;
 7              Console.ReadKey();
 8          }
 9      }

Result: 300

Reason: When the father object is instantiated, the constructor of the parent class will be called first. When assigning a value to the attribute Age in the parent class, since the child class rewrites the attribute in the parent class, the program will skip the assignment to the parent class. Attribute Age assignment

, instead jumping directly to the rewrite of the property in the subclass, which explains that the result is 300 instead of 0.

Conclusion: In a normal project, we will protect the fields (private) to prevent external personnel from rewriting the members in the parent class through subclasses, resulting in serious consequences.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325377169&siteId=291194637