C # Polymorphism

Source: Internet
Author: User

C # polymorphism defines polymorphism: the same symbol or name in the program has different interpretations under different circumstances. There are multiple basic forms of polymorphism: www.2cto.com compile-time polymorphism: refers to the polymorphism that can be determined at the program compilation stage is implemented by the overload mechanism: function overload and operator overload Runtime polymorphism: the polymorphism that can be determined only when the program is running dynamically is implemented by dynamically binding inherited functions with virtual functions. Function overloading: allows many functions to use the same function name in the scope, however, the number of parameters in the form of a function prototype or the type of the corresponding location is different. Virtual functions: virtual functions are virtual-modified member functions in the class. virtual functions are redefined in one or more Derived classes in the public inheritance institution. During the call process, dynamically bind the pointer to the base class. Multi-State implementation uses the parent class implementation to hide or overwrite the inheritance of the method methods of the parent class: subclass can inherit the addition of all the methods of the parent class: Add a new method in the subclass definition, this method overwrites the corresponding functional methods for sub-classes: sub-classes define methods with the same name as parent classes (polymorphism) and define methods with the same name but different parameter lists, a method that defines the same name as a method overload and has the same parameter list. It is called a new method and uses the new modifier, this method is used to hide methods of the parent class with the same definition name and the same parameter list. Methods in the parent class are modified using abstract/virtual. Methods in the subclass are modified using override, the overwrite is called the overwrite of the virtual method, which overwrites the virtual method of the parent class. The PK override method cannot use the static modifier to overwrite the signature of the base class. The virtual method cannot use new and override together with the private method: new is the method for hiding the parent class. If you forcibly convert the instance of the derived class to the instance of the base class, you can still call the hidden class member. Example: DerivedClass B = newDerivedClass (); B. doWork (); // callthe new method. b's DoWork () method BaseClass A = (BaseClass) B;. doWork (); // callthe old method. the DoWork () method override of A is A method that overwrites the parent class and is completely replaced. The method of the parent class must be modified using virtual .. When a derived class overrides a virtual member, the Member is called even if the instance of the derived class is accessed as a base class instance. Example: DerivedClass B = new DerivedClass (); B. doWork (); // Callsthe new method .. b's DoWork () method BaseClass A = (BaseClass) B;. doWork (); // Also callthe new method .. doWork () method of B polymorphism 2: abstract methods and abstract classes abstract modification abstract use notes: abstract methods do not have a method body abstract member. Only abstract classes in abstract classes can have non-abstract member abstract classes. abstract methods must be implemented. abstract classes can only be used as base classes and cannot be instantiated and overwritten: different objects have different behaviors for the same method call (when different objects receive the same message, different actions will be generated) override PK overload is the same method name, if the parameter or parameter type is different, reload multiple times to meet different needs. Overload is a process-oriented concept rewriting. It is the rewriting of functions in the base class. It is an object-oriented conceptual virtual method. PK abstract methods. virtual Methods. virtual Methods. abstract METHODS. abstract METHODS must have a method body, even if a semicolon does not allow a method body, the quilt class override must be a quilt class. override can be written only in the abstract class except for the seal class. 3. The interface keyword and the interface can have attributes, method (not implemented) If a class inherits both the class and the interface, the class must be detached from the front. A class can inherit only one parent class, but multiple interfaces can be implemented. Abstract classes and interfaces are different: you cannot directly instantiate an interface. an interface that does not contain methods can be inherited. A class can only be separated by a single inheritance class. The same points can be split between different source files: interfaces, classes, and structures can all inherit interfaces from multiple interfaces, which are similar to abstract base classes, any non-Abstract type of the inherited interface must be implemented. All member interfaces and classes of the interface can contain events, indexers, methods, and attributes.

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.