The concept of polymorphism: Allowing an object to exhibit multiple states (types)
3 ways to achieve polymorphism: 1, virtual Method 2, abstract Class 3, interface
// 1. Virtual methods
1), virtual method
Steps:
1, the method of the parent class is marked as a virtual method, using the keyword virtual, this function can be the quilt class to write again.
Public classEmployee { Public Virtual voidDaKa () {Console.WriteLine ("Nine-point clock -in"); } } Public classManager:employee { Public Override voidDaKa () {Console.WriteLine ("Manager 11-point Punch -in"); } } Public classProgrammer:employee { Public Override voidDaKa () {Console.WriteLine ("program apes don't punch in"); } }
// Abstract Class 1
2), abstract class
When a method in the parent class does not know how to implement it, consider writing the parent class as an abstract class and writing the method as an abstract method.
Public Abstract classAnimal//Abstract class { Public Virtual voidT () {Console.WriteLine ("I'm a virtual method ."); //if a method in the parent class has a default implementation, and the parent class needs to be instantiated, consider defining the parent class as a normal class, using a virtual method to implement polymorphism. //if a method in the parent class does not have a default implementation and the parent class does not need to be instantiated, the class can be defined as an abstract class. } //6. You can include instance members in an abstract class. //and instance members of an abstract class can be implemented without a quilt class Private int_age; Public intAge {Get{return_age;} Set{_age =value;} } PublicAnimal (intAge ) { This. Age =Age ; } Public Abstract voidBark ();//Abstract Method 1. Abstract members must be marked abstract and cannot have any implementations. Public Abstract stringName//Abstract Properties { Get; Set;// //4. After the abstract attribute subclass inherits the abstract class, it must override all the abstract members in the parent class. } //Public Abstract String teststring (string name);//2. Abstract members must be in an abstract class. //3. Abstract classes cannot be instantiated PublicAnimal () {}//Public void Test ()//{ // //NULL Implementation//} } Public Abstract classTest:animal {//(You can not rewrite the subclass unless it is also an abstract class) } Public classDog:animal//alt +SHIRT+F10 { //Public abstract void Test (); Public Override voidBark () {Console.WriteLine ("the barking of the dog is Wang Wang"); } Public Override stringName {Get { Throw Newnotimplementedexception (); } Set { Throw Newnotimplementedexception (); } } //string//Public override String TestString (string name)//{ // //throw new NotImplementedException (); //} //If there are parameters in the abstract method of the parent class, then. Subclasses inheriting this abstract parent class must pass in the corresponding parameter when overriding the method of the parent class. //If there is a return value in the abstract method of the abstract parent class, the subclass must also pass in the return value when overriding the abstract method. }
//Abstract Class 2 Public Abstract classShape { Public Abstract DoubleGetarea (); Public Abstract DoubleGetperimeter (); } Public Abstract classShape { Public Abstract DoubleGetarea (); Public Abstract DoubleGetperimeter (); } Public classCircle:shape {Private Double_r; Public DoubleR {Get{return_r;} Set{_r =value;} } PublicCircle (Doubler) { This. R =R; } Public Override DoubleGetarea () {returnMath.PI * This. R This. R } Public Override DoubleGetperimeter () {return 2* Math.PI * This. R } } Public classSquare:shape {Private Double_height; Public DoubleHeight {Get{return_height;} Set{_height =value;} } Private Double_width; Public DoubleWidth {Get{return_width;} Set{_width =value;} } PublicSquare (DoubleHeightDoublewidth) { This. Height =height; This. Width =width; } Public Override DoubleGetarea () {return This. Height * This. Width; } Public Override DoubleGetperimeter () {return( This. Height + This. Width) *2; } }
1. Abstract members must be marked abstract and cannot have any implementations.
2. Abstract members must be in an abstract class.
3. Abstract classes cannot be instantiated
4. After the subclass inherits the abstract class, it must override all the abstract members in the parent class.
(You can not rewrite the subclass unless it is also an abstract class)
5. The access modifier for an abstract member cannot be private
6. You can include instance members in an abstract class.
and instance members of an abstract class can be implemented without a quilt class
7. Abstract classes are constructors. Although it cannot be instantiated.
8. If there are parameters in the abstract method of the parent class, then. Subclasses inheriting this abstract parent class must pass in the corresponding parameter when overriding the method of the parent class.
If there is a return value in the abstract method of the abstract parent class, the subclass must also pass in the return value when overriding the abstract method.
======
If a method in the parent class has a default implementation, and the parent class needs to be instantiated, consider defining the parent class as a normal class, using a virtual method to implement polymorphism.
If a method in the parent class does not have a default implementation and the parent class does not need to be instantiated, the class can be defined as an abstract class.
Interface
An interface is a specification.
As long as a class inherits an interface, the class must implement all the members of this interface in order to polymorphic. The interface cannot be instantiated.
That is, the interface cannot be new (cannot create an object)
Members in an interface cannot have access modifiers, the member access modifier in the interface is public, and cannot be modified.
Members in the (default to public) interface cannot have any implementations ("Light says not done", but define a set of non-implemented members).
Interfaces can only have methods, properties, indexers, events, and cannot have fields and constructors.
Interfaces and interfaces can inherit, and can inherit more.
An interface cannot inherit a class, and a class can inherit an interface (an interface can inherit only from an interface, and a class can inherit an interface or inherit a class).
Subclasses that implement an interface must implement all members of the interface.
A class can inherit a class at the same time and implement multiple interfaces, and if a subclass inherits the parent class A and implements the interface IA, then syntactically a must be written in front of the IA.
Class myclass:a,ia{}, because classes are single-inheritance. Display the purpose of implementing the interface: The problem of duplicate name of the workaround
When to display the go to implement Interface:
When the methods and parameters in the inheritance excuse are the same, the implementation interface
When an abstract class implements an interface, subclasses are required to implement the interface.
Public Interfaceiflyable {//the member in the interface does not allow the addition of an access modifier, which is public by default voidFly (); stringTest (); //do not allow to write functions with method body//string _name;//cannot contain fields stringName {Get;//Automatic Properties Set; } } //for polymorphism. //The interface cannot be instantiated. //that is, the interface cannot be new (cannot create an object)Iflyable fly1 =NewBird ();//display implementation interface is to solve the problem of the name of the methodIflyable Fly =NewBird (); Iflyable FLY1=NewBird (); Fly. Fly (); Bird Bird=NewBird (); Bird. Fly (); Public classbird:iflyable { Public voidFly () {Console.WriteLine ("Bird Flying will"); } /// <summary> ///Display Implementation Interface/// </summary> //void Iflyable.fly ()//{ //Console.WriteLine ("I am the fly of the interface"); //} voidIflyable.fly () {Console.Write ("I am the interface of the fly"); } } Public Interfaceiflyable {voidFly (); }
Three ways of C # basic polymorphism