The multi-state of the revision series

Source: Internet
Author: User

definition of a polymorphic:

The same action acts on different classes of objects, different classes of objects are executed differently, resulting in different execution results. Easy to understand: Let an object show many types

two implementation of polymorphic (virtual method):

(a) definition:

The method declared in the base class with the virtual keyword is called a virtual method, and can be overridden in a derived class by the override keyword

(ii) use of the scene:

When you explicitly define a base class, you can consider using a virtual method when overriding a method in a base class.

(c) examples:

classPerson {Private string_name;  Public stringName {Get{return_name;} Set{_name =value;} }                 PublicPerson (stringname) {             This. Name =name; }          Public Virtual voidSayHello () {Console.WriteLine ("I am a human being"); }    }    classChinese:person { PublicChinese (stringname):Base(name) {} Public Override voidSayHello () {Console.WriteLine ("Hello, my name is {0}, I am Chinese", This.        Name); }    }     classJapanese:person { PublicJapanese (stringname):Base(name) {} Public Override voidSayHello () {Console.WriteLine ("Hello, my name is {0}, I am Japanese", This.        Name); }           }    classProgram {Static voidMain (string[] args) {Chinese C1=NewChinese ("Xiao Ming"); Chinese C2=NewChinese ("Xiao Li"); Japanese J1=NewJapanese ("Jing Quan Yiliang"); Japanese J2=NewJapanese ("Turtle Sumida"); Person[] Pers={c1, c2, J1, J2}; //Pers[0] = C1; //pers[1] = C2; //pers[2] = J1; //pers[3] = J2; Another way to write an array above             for(inti =0; i < pers.length; i++) {Pers[i].            SayHello (); }            //Person p = new Chinese ("Xiaoming");//Japanese ("Bingbian"); //P.sayhello ();Console.readkey (); }    }

three how to implement a polymorphic (abstract method)

(a) Definition:

The class that describes the generality is an abstract class, and the methods in the abstract class are decorated with the abstract keyword. The abstract class does not take into account the specific way in which it is implemented.

(b) Members:

abstract methods and non-abstract methods; attributes; fields; constructors; instance members

(iii) Use of the scene :

1 before the abstract class definition, if there is no common base class in other classes, then the common method can be achieved by abstracting out the shared base class.

2 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 generic class, using virtual methods to implement Polymorphic

(iv) Examples :

classProgram {Static voidMain (string[] args) {Animal an=NewDog (); An.            Speak ();        Console.readkey (); }    }     Public  Abstract  classAnimal { Public Abstract voidSpeak (); }     classDog:animal { Public Override voidSpeak () {Console.WriteLine ("Barking dogs bark."); }    }     classCat:animal { Public Override voidSpeak () {Console.WriteLine ("the Cat meow Meow is called"); }     }

(v) Precautions:

1. Abstract class members must be marked abstract and cannot have any implementations, abstract members must be in the abstract class

2. Abstract classes cannot be instantiated

3. When a subclass inherits an abstract class, it must override all the abstract members in the parent class (unless the subclass is also an abstract class, you do not need to override it)

4. The access modifier for an abstract member cannot be private

5. An instance member can be included in an abstract class, and an instance member of an abstract class can be implemented without subclasses

6. There are constructors in abstract classes, although they cannot be instantiated

7. If the parameter is in the abstract method of the parent class, then the subclass inheriting the 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.

8. 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 generic class, using virtual methods to implement Polymorphic

9. If the method in the parent class does not have a default implementation. The parent class cannot be instantiated, you can define the class as an abstract class

Four implementation of polymorphic (interface)

(i) Definition of the interface:

interface is a programming specification, the ability,C # itself does not support multiple inheritance of classes, but through the interface can implement multiple inheritance of the class

(b) Members:

a method, an indexer, an event, an automatic attribute (a property without a field or method body), a constructor and a field

(iii) use of the scene:

to inherit multiple base classes, the base class can be abstracted as an interface

(iv) examples:

     classProgram {Static voidMain (string[] args) {ichlssable ch=NewTeacher (); Ch.            CHLSS ();        Console.readkey (); }    }     Interfaceichlssable {voidCHLSS (); }            classDriver { Public voidKaiche () {Console.WriteLine ("I'm a driver, I can drive."); }    }     classteacher:driver,ichlssable { Public voidTeach () {Console.WriteLine ("I am a teacher, can teach"); Base.        Kaiche (); }         Public voidChlss () {Console.WriteLine ("I am human, can eat and drink and sleep"); }            }

(v) Precautions:

1. A class inherits an interface, and this class needs to implement all the members of the interface

2. The interface cannot be instantiated, that is, the object cannot be created with new

3. The member of the interface cannot add access modifiers, the default is public, and cannot be modified, the members of the interface cannot have any implementations

4. Interfaces and interfaces can inherit, and can inherit multiple

5. An interface cannot inherit a class, and a class can inherit an interface (interfaces inherit only classes, classes can inherit classes and interfaces)

6. Subclasses that implement an interface must implement all members of the interface

7. A class can inherit a class and multiple interfaces at the same time, then the name of the inherited class must be written in front of the interface name and separated

8. The purpose of the display implementation interface is to solve the renaming problem of the method, when the method and parameters in the inherited interface are exactly the same as u, it is necessary to use the implementation interface of the display. , when an abstraction implements an interface, subclasses are required to implement the interface.

9. The implementation method cannot be included in the interface

The multi-state of the revision series

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.