Polymorphism (virtual method)
1, in the parent class method to add virtual, this method can be a quilt class rewrite.
Join override before the method of a subclass
Polymorphism means that different objects receive the same message, which produces different behavior, and the same class shows different behavior characteristics in different situations.
The role of polymorphism: the different subclasses are considered as the parent class, you can mask the differences between different subclass objects, write common code, and make common programming to meet the changing needs.
Summarize:
A, the parent class if a method requires a subclass override, you can mark the method as a virtual method;
B, virtual methods must be implemented in the parent class, the null implementation can also (with the abstract class difference);
C, virtual method subclasses can be overridden or not overridden (unlike abstract classes).
static void Main (string[] args) {Teacher te = new Teacher ("Teache_w");//This is the parent class itself, creating the parent class object, or, if it is an abstract class or interface, the parent class
Cannot create object; Student st = new Student ("Stud_w"); Teacher TT = (teacher) St; This assigns the subclass to the parent class, Tt.sayname ();
The parent class invokes the method of the parent class, but this method is Console.readkey (the parent class is hidden), so the method to invoke the subclass is called ();
The public class Teacher {//Because this class needs to be created, the method is marked as virtual string _name;
public string Name {set{_name=value;}
Get{return _name;} Teacher (string name) {this.
name = name; public virtual void Sayname () {//... Virtual Parent Class: This method makes sense in the parent class, and the parent class can invoke Console.WriteLine ("I am {0}", this.)
Name); The public class Student:teacher//inherits the parent class {public student (string name)//Call
Constructors for parent classes : base (name) {} public override void Sayname ()//Overrider parent class { Console.WriteLine (' My name is {0} ', this.
Name); }
}
}
Abstract class
When the method of the parent class does not know how to implement, consider writing the parent class as an abstract class, and writing the parent class method as an abstract class method
Abstract methods do not allow method bodies, such as public abstract void add ();
Summarize:
1. Abstract members must be in abstract classes, and abstract classes can include non-abstract members;
2, abstract class can not be instantiated;
3, abstract class Mark abstract, cannot have any realization, cannot have the method body;
4. When a subclass inherits an abstract class, all the abstract members of the parent class must be overridden, and if the subclass is an abstract class, it cannot be written;
5. An abstract class can contain instance members, and instance members can be implemented without the quilt class. Although an abstract class cannot be instantiated, its instance members can be inherited;
6. Abstract classes are constructors, although they cannot be instantiated;
7. Subclasses override the parent class's method, and the parameters and return values must be the same as the parent class;
8, if the parent class has a default implementation, and the parent class needs to be instantiated, so the parent class is defined as a common class, using virtual methods to achieve polymorphism, if there is no default implementation in the parent class, and the parent class does not need to be instantiated, you can use an abstract class;
9, the function of abstract class is to let subclass inherit;
10, the abstract method can not be decorated with static.
static void Main (string[] args)
{
Teacher1 te = new Teacher1 (); This sentence is wrong, the abstract class is not allowed to create objects, the interface is not allowed to create objects, because the object's method is not, create also did not use
student1st = new Student1 ();
Teacher1 TT = (teacher1) st;
Teacher1 te = new Student1 (); The above two words can also use this sentence to replace
Tt.sayword (); The method of the parent class is invoked, but the method of the parent class is overridden, so the method
Console.readkey () for the final invocation of the subclass;
Public abstract class Teacher1
{public
abstract void Sayword ();
public
class Student1:teacher1
{public
override void Sayword ()
{
Console.WriteLine ("Abstract Student Class");
}