C # polymorphism Theory Foundation

Source: Internet
Author: User
Tags define abstract

I. Overview

The same action acts on different objects, can have different interpretations, and produces different execution results, which is polymorphism.

The polymorphism in the component can be implemented in different ways:

Interface polymorphism.
Inherit polymorphism.
The polymorphism implemented by an abstract class.

Second, the realization of 2.1, virtual method

Definition: When a method declaration in a class is preceded by a virtual modifier, it is called a virtual method.

Role: Subclasses can override virtual methods in a parent class, and virtual methods are a manifestation of polymorphic properties.

That is, using the virtual keyword to modify the parent class method, with the override keyword modifier Subclass method, implements the method rewrite.

Characteristics:

A), no static,abstract , or override modifiers are allowed after using the virtual modifier

b), the access level of the parent class method, and the access level of the subclass override method must be the same, i.e. they should have the same access modifier

2.2. Abstract classes and abstract methods

Description: If a class is not associated with a specific thing, but simply expresses an abstract concept, just as a base class for its derived class, such a class is an abstract class, and when you declare a method in an abstract class, it is an abstract method if you add abstract.

Abstract classes are special classes that cannot be instantiated, except that they have other characteristics of classes, and it is important that abstract classes include abstract methods, which are not available to ordinary classes.

Abstract methods can only be declared in an abstract class and do not contain any implementations, and derived classes must overwrite them. In addition, abstract classes can derive from an abstract class that can override the base class's abstract methods or not, and if not overwritten, their derived classes must overwrite them.

Features of 2.2.1 and abstract classes:

A), abstract class cannot be instantiated ( cannot use new operation )

b), abstract classes can contain abstract methods and abstract accessors

c), abstract class cannot be sealed or static ( cannot add static or sealed in front of abstract class)

d), non-abstract classes derived from abstract classes must implement all inherited abstract methods and abstract accessors

e), abstract class is a description of the subclass, only the Quilt class inheritance, have practical significance

2.2.2, abstract class restrictions on constructors:

A), do not define a public or protected internal constructor in an abstract class (a constructor that has common or protected internal visibility for types that can be instantiated, Abstract types cannot be instantiated under any circumstances)

b), a protected (protected) or private constructor should be defined in the abstract class

c), if a protected (protected) constructor is defined in an abstract class, the base class can perform initialization when an instance of the derived class is created.

Features of 2.2.3 and abstract methods:

Definition: in an abstract class , a method declares that the method is abstract if it is added with the abstract modifier

A), the abstract method is an implicit virtual method. (an abstract method can be thought of as a virtual method without a method body)

b), allow only abstract method declarations in abstract classes

c), because the abstract method declaration does not provide the actual implementation, all have no method body. (that is, the method declaration ends with a semicolon, and there is no curly brace {} After signing, the implementation is provided by the overridden method, which is a member of the non-abstract class)

D), cannot use static or virtual modifiers in abstract method declarations

e), except in the syntax of declarations and invocations, the behavior of abstract attributes is the same as abstract methods

f), cannot use the abstract modifier on a static property

g), in a derived class, you can override an abstract inherited property by including a property declaration that uses the override modifier

[Note]:

Animal a = new Dog;

A.cry ();

A.show;

Animal: Abstract class, which has an abstract method cry (), a concrete method show ()

Dog: Inherits the parent class animal and uses override to override the abstract method cry (), and has a subclass of its own specific method Showdog ()

Thus the conclusion can be drawn:

1, does not instantiate the abstract class, just declares an abstract class object, the instantiation must be implemented by the constructor, and here is the subclass constructor, so the subclass object is instantiated, but the abstract object refers to the subclass instance

2, the abstract class object calls the subclass of the Cry () method and its own show method, the subclass of the Showdog () method is not accessible

3, the abstract class object refers to the subclass instance, basically still belongs to the abstract class, the ability to access the subclasses of the overridden methods and the parent class own properties and methods, the subclass's own properties and methods are inaccessible.

2.3. Interface

Describe:

a), in. NET, an interface is a specification and a standard, and is purely a protocol.

b), the interface does not provide a default implementation, and a particular member defined by the interface relies on the exact behavior it simulates. An interface represents the behavior that a class or struct can choose to implement.

c), from the syntax, the interface is defined using the interface keyword. Unlike classes, interfaces do not specify a base class, but interfaces can specify a base interface.

D), the member of the interface does not specify an access modifier (because all interface members are implicitly public and abstract)

e), the interface has no effect unless it is implemented by a class or interface.

f), a direct base class must be the first item after the colon operator.

g), the realization of the interface is a "or all, or do not" proposition.

h), when a class inherits an interface, it not only implements all the methods defined by the interface, but also implements all the methods that the interface inherits from the other interfaces.

i), in C #, a class can implement multiple interfaces, although it cannot implement multiple inheritance.

j), the interface is a reference type, similar to a class.

k), the interface basically does not have any specific characteristics of inheritance, it only promises to be able to invoke the method;

L), a class may implement several interfaces at a time, but only one parent class can be extended.

m), a good interface definition should be functional, rather than multi-functional, or cause interface pollution. If a class just implements one of the functions of this interface, and has to implement other methods in the interface, it is called interface pollution.

2.4. Differences between 2.4.1, abstract methods and virtual methods:

The 1> virtual method has an implementation part and provides an option for the derived class to overwrite the method, and the abstract method does not provide the implementation part, forcing the derived class to overwrite the method

The 2>abstract method can only be declared in an abstract class, but the virtual method is not

The 3>abstract method must be overridden in a derived class, and the virtual method does not have to

The 4>abstract method cannot declare the method body, and the virtual method can

5> classes containing the abstract method cannot be instantiated, while classes containing the virtual method can

The difference between 2.4.2, interfaces, and abstract classes:

Same point:

1> cannot be instantiated directly, they can be implemented by inheritance.

2> contains a method declaration that is not implemented

Different points:

The 1> interface supports multiple inheritance, and abstract classes cannot implement multiple inheritance.

The 2> interface can only define abstract rules, and abstract classes can define rules as well as provide implemented members.

An 3> interface is a set of behavior specifications; An abstract class is an incomplete class.

The 4> interface can be used to support callbacks, and abstract classes cannot implement callbacks because inheritance is not supported.

The 5> interface can be used for value types and reference types, and an abstract class can only be used for reference types. (for example, astruct can inherit an interface and not inherit a class)

6> the concrete methods implemented by the abstract class are virtual by default, but the interface method in the class that implements the interface defaults to non-virtual, and of course you can declare it as virtual.

7> abstract class implements a principle in oop, separating mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable is implemented as subclasses.

8> Microsoft's custom interface is always followed by the able field, proving that it is a class of expression "I can do ... ”。 Abstract classes are more defined in a series of tightly related classes, whereas interfaces are mostly in classes that have a loose relationship but implement a function.

9> (interfaces) are similar to non-abstract classes where an abstract class must provide its own implementation for all members of the interface listed in the base class list of the class. However, an abstract class is allowed to map an interface method to an abstract method.
10> if an abstract class implements an interface, you can map a method in an interface to an abstract class as an abstract method without having to implement an interface method in a subclass of an abstract class.


Use of 2.4.3, abstract classes, and interfaces:

1. You can create an abstract class if you anticipate that a version problem will occur.

2. "Interface unchanged" is an important factor to be considered. Therefore, when an extension is added by an interface, the new interface should be added instead of changing the existing interface.

3. Abstract classes are primarily used for closely related objects, while interfaces are suitable for providing common functionality for unrelated classes.

4. Interface multi-definition object behavior, abstract class multi-definition object properties.

5. As far as possible to design the interface as a function of a single functional block, for example, the. NET framework, IDisposable, IComparable, and so on contain only one public method.

Mo Xuan June Source:http://www.cnblogs.com/yuan-jun/p/6413211.html This article copyright belongs to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph of the statement, And in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

C # polymorphism Theory Foundation

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.