One: What is polymorphism?
Multiple forms, both different objects are different for the same operation.
Ii. several considerations for abstract classes
1, abstract class using the abstraction modifier
2, abstract methods can only be found in abstract classes
3, abstract classes cannot be instantiated
4, there is no method body for abstract methods
5, abstract class can not be static class or sealed class
6, subclasses must override all the abstract methods of the parent class, unless the subclass is also an abstract class
7, there can be ordinary methods in the abstract class
8, abstract can have a constructor function
9, abstract methods in abstract classes are intended to constrain the method form of subclasses.
Three: "Instantiation" of an abstract class
Although the abstract class itself cannot be instantiated by new, he can point the Reference object to the real object of the subclass, or it can be called an indirect instantiation.
Person as parent class
Public abstract class person{public int Age {get; set;} public string Name {get; set;} public person (int age,string name) {this. Age = Age;this. name = name;} public abstract void Say ();p ublic void Eat () {Console.WriteLine ("I am the parent class");} }
Student class to inherit person
public class student:person{Public Student (String Name,int age) {public Student (int., String name): Base (age , name) {this . Age = age; This. name = name; } public override void Say () { Console.WriteLine ("Subclass Speak"); } public void Eat () {Console.WriteLine ("I Am a Subclass");}}}
When a parent class object points to a true object of a subclass, the subclass first walks the constructor of the parent class, and assigns a value to its properties in the constructor of the child class. ,
Person p = new Student (18, "Tommy"); P.say (); In this case, the parent class variable points to the subclass object, calls the method of the subclass, //conforms to polymorphism, the parent class and the subclass method call the method P of the child class. Eat (); If no method does not have a relationship, the method of the parent class is called by default. Student stu = (Student) p; Stu. Eat (); If you want to invoke a specific method of a subclass, you need to convert the type, called in Java, and Console.readkey () down.