C # object-oriented -- polymorphism,

Source: Internet
Author: User

C # object-oriented -- polymorphism,

There are three types of polymorphism: 1. Virtual Method

2. abstract class

3. Interface

1. Virtual Methods
1. Mark the method of the parent class as a virtual method and use the keyword virtual. This function can be rewritten by the quilt class.

For example:

Class Program {static void Main (string [] args) {Chinese cn1 = new Chinese ("Han Mei"); Chinese cn2 = new Chinese ("Li Lei "); american a1 = new American ("Kobe Bryant"); American a2 = new American ("o'neill"); Person [] pers = {cn1, cn2, a1, a2 ,}; for (int I = 0; I <pers. length; I ++) {pers [I]. sayHello ();} Console. readKey () ;}/// <summary> // parent class -- human // </summary> public class Person {private string _ name; public string Name {get {return _ name;} set {_ name = value;} public Person (string name) {this. name = name;} public virtual void SayHello () {Console. writeLine ("I am a human") ;}/// <summary> /// subclass -- Chinese // </summary> public class Chinese: person {public Chinese (string name): base (name) {} public override void SayHello () {Console. writeLine ("I am a Chinese, my name is {0}", this. name) ;}/// <summary> // subclass -- American // </summary> public class American: Person {public American (string name ): base (name) {} public override void SayHello () {Console. writeLine ("My name is {0}, I'm a Chinese man", this. name );}}View Code

2. abstract class
When the methods in the parent class do not know how to implement them, you can consider writing the parent class into an abstract class and writing the methods into an abstract method.

Class Program {static void Main (string [] args) {// The name of a Dog is Animal a = new Cat (); // new Dog ();. bark (); Console. readKey () ;}/// <summary> // parent class -- Animal /// </summary> public abstract class Animal {public virtual void T () {Console. writeLine ("Animal Declaration");} private int _ age; public int Age {get {return _ age;} set {_ age = value ;}} public Animal (int age) {this. age = age;} public abstract void Bark (); public abstract string Name {get; set;} public Animal () {}}/// <summary> // subclass -- Test /// </summary> public abstract class Test: animal {} // <summary> // subclass -- Dog // </summary> public class Dog: Animal {public override void Bark () {Console. writeLine ("Dog trademanager");} public override string Name {get {throw new NotImplementedException ();} set {throw new NotImplementedException ();}}} /// <summary> // sub-class -- Cat // </summary> public class Cat: Animal {public override void Bark () {Console. writeLine ("");} public override string Name {get {throw new NotImplementedException ();} set {throw new NotImplementedException ();}}}View Code

Abstract class features:

1) abstract members must be marked as abstract members and cannot be implemented.

Public abstract class Animal
{

Public abstract void Bark ();

}

2) The abstract member must be in the abstract class.
3). abstract classes cannot be instantiated.

4) after the subclass inherits the abstract class, all abstract members in the parent class must be overwritten.

(Unless the subclass is also an abstract class, you can not override it)
5) The access modifier of the abstract member cannot be private.
6). The abstract class can contain instance members.
And the instance members of the abstract class can be implemented without the quilt class.

7). abstract classes have constructors. Although it cannot be instantiated.


8) If the abstract method of the parent class contains parameters. The subclass that inherits this abstract parent class must input corresponding parameters when rewriting the parent class method.

If there is a return value in the abstract method of the abstract parent class, the subclass must also input the return value when rewriting this abstract method.

3. Interface

Class Program {static void Main (string [] args) {IFlyable fly = new Bird (); // new Person (); fly. fly (); Console. readKey () ;}} public class Person: IFlyable {public void Fly () {Console. writeLine ("Human flying") ;}} public class Student {public void Fly () {Console. writeLine ("Human flying") ;}} public class Bird: IFlyable {public void Fly () {Console. writeLine ("bird flying") ;}} public interface IFlyable {// access modifier is not allowed. The default value is public // method, and the automatic attribute void Fly ();} public interface M1 {void Test1 ();} public interface M2 {void Test2 ();} public interface M3 {void Test3 ();} public interface SupperInterface: M1, M2, m3 {} public class Car: SupperInterface {public void Test1 () {throw new NotImplementedException ();} public void Test2 () {throw new NotImplementedException ();} public void Test3 () {throw new NotImplementedException ();}}View Code

An interface is a specification.
As long as a class inherits an interface, this class must implement all the members of this interface.

For polymorphism. The interface cannot be instantiated.
That is to say, the interface cannot be new (the object cannot be created)

The "access modifier" cannot be added to the interface members. The access modifier of the interface members is public and cannot be modified.


(The default value is public) the members in the interface cannot have any implementations ("simply do not", but define a group of unimplemented members ).


Interfaces can only contain methods, attributes, indexers, and events, and cannot contain "fields" and constructors.

Interfaces and interfaces can be inherited.

An interface cannot inherit a class, but a class can inherit interfaces (an interface can only inherit interfaces, and a class can inherit both interfaces and classes)

The subclass that implements the interface must implement all the members of the interface.

A class can inherit A class and implement multiple interfaces at the same time. If A subclass inherits the parent class A and implements the interface IA, A must be written before IA in syntax.


Class MyClass: A, IA {}, because the class is single-inherited.


Display implementation interface purpose: Solve the Problem of duplicate names of methods
When to display the de-implementation interface:
When the methods and parameters in the inheritance excuse are the same, use the displayed implementation Interface


When an abstract class implements an interface, a subclass is required to implement the interface.


======
If the methods in the parent class have default implementations and the parent class needs to be instantiated, you can define the parent class as a common class and use the virtual method to implement polymorphism.

If the methods in the parent class are not implemented by default and the parent class does not need to be instantiated, the class can be defined as an abstract class.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.