Primary:
Subclass contains all property methods of the parent class
All subclasses can be converted directly to the parent class type
When a parent class type variable contains an object of a subclass, it can be converted to that subclass type.
The parent class and the child class are loaded:
New= (Ren) m; // Man subclass converted to Ren parent class New = (man) r; // when converted to a parent class and then converted to a subclass, it can only be converted to the original subclass, and cannot be converted to another child class
You need to use forced reload.
Private and protected:
Private can only be used as a member variable in the current class.
Protected can only be used as member variables in the current class or in subclasses.
When the subclass is initialized, the parent class is initialized first, and the constructor in the parent class is executed.
Write in the parent class:
private int hello;// private can only be used as a member variable in its own class protected
int world; // protected can only be used as a member variable in its own class and subclass public Ren () // constructor (first executed) {world + = 10 ; Hello = 0 ; public void Ren1 () {Console.WriteLine (World); }
In a sub-class:
class Man:ren// plus ": ren" subclass, indicating that man is a subclass of Ren. { public Mans () { 5/// You can use protected variables in subclasses. You cannot use the private variable. } publicvoid man1 () { Console.WriteLine (World) ; } }
In another sub-class:
class Women:ren// plus the ": Ren" subclass, which indicates that women is a subclass of Ren. { public Women () { 7; } Public void women1 () { Console.WriteLine (world); } }
Main function
Static voidMain (string[] args) {//subclass contains all property methods of the parent class//all subclasses can be converted directly to the parent class type//when a parent class type variable contains an object of a subclass, it can be converted to that subclass type. Ren rr =NewRen (); Rr.ren1 ();//The print result is tenMans M=NewMan (); M.man1 ();//the printed result isWomen W=Newwomen (); W.women1 ();//The print result isConsole.ReadLine (); }
When static is added to the parent class
Private intHello//private can only be used as member variables in your own class protected Static intWorld//protected can only be used as member variables in their own classes and subclasses//note here that a static static variable (protected) has been added, unlike above PublicRen ()//Constructors (performed first){ World+=Ten; Hello=0; } Public voidRen1 () {Console.WriteLine (World); }
Output Result:
New Ren (); Rr.ren1 (); // The print result is ten Man m=new man (); M.man1 (); // The print result is New Women (); W.women1 (); // printing results are
Its principle:
The protected variable will not change every time the initialization (new) is initialized.
10+15=25; (the first 10 is a change in the initialization of Ren)
25+17=42; (25 is the change when the man is initialized)
Method calls to the parent class and child classes:
When a subclass has a method with the same name in its parent class, the new is the one whose method is called.
Parent class:
Public void Shuohua () { Console.WriteLine (" speak:"); }
Man sub-category:
Public void Shuohua () { Console.WriteLine (" English "); }
Note Two is a method with the same name.
In the main function:
M.shuohua (); //Call the method in man Print Result: English Rr.shuohua (); //Call is in the Ren method Printing Results: Speaking: = (Ren) m; // The man class M is coerced into the Ren class. Rr.shuohua (); the method that is called in Ren prints the result: Speak: = (man) RR; cast The Ren class RR into the man class. M.shuohua (); Call the method in man print Result: English
20141214--c# Parent class, subclass