You may have seen the dog's blood "inheritors", OK, in the process of inheritance, there is the point of ' inheritance ' meaning, but not the same. Give a small Li Zilai said: Your grandfather's money, sooner or later is your father's, your father's, and sooner or later is your. This is inheritance. And what about the succession in the program?
To know how inheritance is going, first figure out why you have to inherit this thing, that is, the purpose. An example might be better understood. For example, you are writing a class < Human, the attribute has name, gender, age. And you need to write another class < student class, the student naturally also has the name, gender and age, but also has his unique attributes (such as grade), do you want to write the common attributes again? Of course it doesn't look like much code, but when you write 10 classes, is there a bright of code redundancy?
So, the (attention to) inheritance came.
Inheritance is something that allows you to write less code , lazy people push progress (vulgar understanding haha). The student class is a subclass of human (derived class ), and Human is the parent class (base class). subclasses inherit the properties and methods of the parent class (do not inherit private fields) and can use these members in subclasses . When creating a subclass object, the object of the parent class is created in the subclass, and the default call to the parent class of the parameterless constructor (something that is easy to initialize).
How to use: Subclass after : (parent class name)
There is also a problem, if the parent class is written with a constructor method, the original default parameterless construction method will be killed, such as the creation of a parent class object in the class will be an error. What do you do?
1) Re-write an argument-free constructor (low)
2) Use the base keyword to display the constructor of the parent class in the child class
Note that theobject class is the base class for all classes.
What if the method of the subclass is the same as the method name of the parent class? Be warned. Then you can't catch the way of the parent class. If you are deliberately doing this, use the New keyword in the subclass method. such as: Public new void Test ()
Ok?
C # Object-oriented---what is inheritance?