The first article of the 2016, let me first spray the feelings of the new Year, ..., OK, spray almost.
In object-oriented, we need to have a horizontal and vertical understanding of the class, different classes have distinct members, and in the inheritance of different classes, the subclass gets the members of the parent class are also colorful.
First Overview:
1. Abstract class
Not all methods in an abstract class are abstract methods, and for purely abstract classes, it is the instantiation of a restricted class, and there can be no abstract method. Not even any members are fine.
2. Abstract methods
The purpose of the abstract method is to let the derived class implement its method, the abstract method is not implemented, the abstract method has no method body, and no curly braces. Classes that contain abstract methods must be abstract classes, so having abstract methods (attributes) is a sufficient and non-essential condition for the class to be abstract.
3. Virtual Methods
A virtual method is a method that is not abstract, it has curly braces, is implemented, subclasses can be overridden, and can be overwritten. Virtual methods are not necessarily inherited into subclasses in the parent class, and are determined by subclasses that, unlike abstract methods, virtual methods can be inherited by subclasses, as opposed to generic methods.
On the code:
1. Life Classes
1 Public Abstract classLife//Abstract class2 { 3 Public stringname;4 5 Public Abstract voidLive ();6 7 Public Abstract voiddeath ();8 9 Public Virtual voidBirth ()Ten { OneConsole.WriteLine ("Birth () virtual methods for life classes"); A } - - Public voidHuozhe () the { -Console.WriteLine ("the Huozhe () method of the Life class"); - } - PublicLife () + { -Console.WriteLine ("Life is constructed"); + } A}
View Code
2. Animal class animal Inherit life
1 Public classAnimal:life2 {3 PublicAnimal ()4 {5Console.WriteLine ("Animal is constructed");6 }7 Public stringChi;8 9 //subclasses must implement all the abstract methods in the parent class, that is, abstract methodsTen One Public Override voidLive () A { -Console.WriteLine ("the animal class must implement the live () abstract method of Live"); - } the Public Override voiddeath () - { -Console.WriteLine ("the animal class must implement the death () abstract method of Live"); - } + - Public Override voidBirth ()//overriding virtual methods in parent life with override//after rewriting will be treated as a real method + { AConsole.WriteLine ("animal complete rewriting of the birth () virtual method in the Life class"); at } - - Public New voidHuozhe ()//when you instantiate a method that overrides Huozhe () in the parent life, use this method - { -Console.WriteLine ("Animal alive."); - } in}
View Code
3. Human people seal class inheritance animal
1 //overrides and overrides are different in form2 Public Sealed classPeople:animal3 {4 Public voidShuo ()5 {6Console.WriteLine ("the Shuo () method in people");7 }8 9 Publicpeople ()Ten { OneConsole.WriteLine ("people is constructed"); A } - Public New voidBirth ()//overwrite the birth () virtual method in animal completion life with the rewritten birth () (which has become a real method) - { theConsole.WriteLine ("Animal overridden Birth () virtual method in people"); - } - Public Override voidDeath ()//people can also rewrite the animal of Death's life extraction method - { +Console.WriteLine ("People implementation of the death () abstract method of life in animal"); - } + Public New voidHuozhe ()//when a method that overrides the Huozhe () in the parent class animal is instantiated, the method A { atConsole.WriteLine ("people alive."); - } -}
View Code
4. Female human woman cannot inherit people
Start testing
1 //Overriding method overrides that do not change the parent class will change the method of the parent class when the parent class is created as a subclass.2 class Program3 {4 Static voidMain (string[] args)5 {6People p1 =Newpeople (); Life is constructed animal is constructed people is constructed7 //Life S1 = new Life (); Abstract classes, as abstract, cannot be declared8Animal LIV1 =NewAnimal ();//Life is constructed animal is constructed9Animal P2 =NewPeople ();//Parent class Calls child class objectTenLiv1.death ();//in the output animal OneLiv1.huozhe ();//in the output animal ALiv1.live ();//in the output animal -Liv1.birth ();//in the output animal -Console.WriteLine ("---------------------"); thePeople p1 =Newpeople (); -P1.death ();//in the output people -P1.huozhe ();//in the output people -P1.live ();//the output inherits from the animal//because there is no rewrite +P1.birth ();//in the output people -Console.WriteLine ("---------------------"); +Liv1.death ();//in the output animal ALiv1.huozhe ();//in the output animal atLiv1.live ();//in the output animal -Liv1.birth ();//in the output animal -Console.WriteLine ("---------------------"); -P2.death ();//in the output people -P2.huozhe ();//in the output animal -P2.live ();//in the output animal inP2.birth ();//in the output animal -Console.WriteLine ("-----------------------"); toConsole.WriteLine ("---------Conversion----------"); + - the //people p3 = new animal (); //An error The subclass reference cannot directly reference the parent class object unless the data type of the parent class object is cast to subclass * //people P4 = (people) Liv1;//If you create an instance without referencing the parent class to the child class object, you cannot convert it. $ //P2.shuo (); //error, cannot be referenced, new method in subclass cannot be referenced when parent class references self-mine objectPanax NotoginsengPeople P5 = (people) P2;//This way, the new method in the subclass can be called, stating that the parent class reference is not used to throw the new method into the subclass, but cannot call the - P5.shuo (); the Console.readkey (); + } A}
View Code
Test results
Well, ponder it!
An exploration of abstract classes, abstract methods, and virtual methods in C #