Before using the abstract method to achieve polymorphism, we must know some knowledge points:
1. Abstract classes cannot be instantiated;
2. Abstract classes can contain non-abstract members, which can be called by their subclasses.
We can first create an abstract class of person, with the following code:
1 Abstract class Person2 {3 4 PublicPerson () {}5 6 PublicPerson (stringNameintAge )7 {8Name =name;9Age =Age ;Ten } One stringName A - Public stringName - { the Get{returnname;} - Set{name =value;} - } - intAge ; + - Public int Age + { A Get{returnAge ;} at Set{age =value;} - } - - Public Abstract voidShow (); -}
In the person class we define an abstract method show (), noting that the abstract method can only be in an abstract class, and that it has no method body.
Next, write two of the two subclasses that inherit the person class, with the following code:
1 classStudent:person2 { 3 Public Override voidShow ()4 {5Console.WriteLine ("I am the Student class's Show method: {0},{1}", name,age);6 }7 }8 classTeacher:person9 {Ten One Public Override voidShow () A { -Console.WriteLine ("I am the Teacher class's Show method: {0},{1}", name,age); - } the}
When subclasses inherit abstract classes:1. The override method must add the keyword override keyword, otherwise it is not considered an overriding method
The method signature of the 2.override method must be exactly the same as the method of the parent class
The 3.override method must find the parent class method that can be overridden
4. Abstract method subclasses must override
Abstract class and subclass are finished, then the implementation of polymorphic, polymorphic use of the general three kinds of ways:
1. Declaring a parent class variable, instantiating a subclass object
2. Parent class as a parameter, passing in the Child class object
3. The parent class is the return value of the method, returning the specific subclass object
Implement the polymorphic code as follows:
1 Static voidMain (string[] args)2 { 3Student stu =NewStudent () {name="AA", age= - };4Teacher tea =NewTeacher () {name="BB", age= +};5 //There is no person object created here. Just use it as a type to create an array object6Person[] Objs=Newperson[2];7objs[0] =Stu;8objs[1] =tea;9 Ten foreach(Person iteminchObjs) One { AItem. Show ();//Polymorphism refers to the response of different sub-class objects, polymorphism is the polymorphism of behavior - } - Console.readkey (); the}
Finally, abstract classes can also inherit abstract classes, but we know that subclasses of inherited abstract classes must override abstract methods of abstract classes, and when an abstract class inherits an abstract class, the inherited abstract method must be added with an override. The code is as follows:
1 Abstract class Manager:person 2 {3 // Subclass If you do not want to override the method of the parent class, you must also add the override keyword, but you can declare it as an abstract method without actually rewriting 4 Public Abstract Override void Show (); 5 }