Lecture 10 interface inheritance and Polymorphism
Interface
??????? The contract of Interface Definition object members is an indispensable part of modern component programming. C # Use the keyword "interface" to create an interface. As a type, an interface also has five types of access modifiers and new redefinition modifiers. The interface can contain four types of members: method, attribute, event, and indexer. The interface itself can only declare these Members, and it does not have to or provide specific implementation of these members. C # It is not allowed to perform any access modification for interface members. The default access modification for interface members is public. The interface is a member contract we have defined for other types, access restrictions on them do not meet the original intention of our application interface. Similarly, C # does not allow "abstract, virtual, override, static" Modification on interfaces. The following is a typical example of interface type declaration:
Public Delegate void stringlistevent (istringlist sender );
Public interface istringlist
{
??? Void add (string S); // Method
??? Int count {Get;} // read-only attribute
??? Event stringlistevent changed; // event
??? String This [int Index] {Get; set;} // Indexer
}??
???????
Note that the interface name is written as an istringlist. The uppercase letter "I" is only the naming convention of the. NET platform, rather than required.
??????? An interface, as a Type Used for contract, cannot be instantiated as a class. We say that a data is an interface type, and we mean that this type implements an interface. The class or structure in C # can implement multiple interfaces. The implementation of the interface type must provide the Implementation of corresponding interface members.
Interface icloneable
{
??? Object clone ();
}
Interface icomparable
{
??? Int compareto (object other );
}
Class listentry: icloneable, icomparable
{
??? Public object clone () {...} // implementation of the icloneable Interface
??? Public int compareto (object other) {...} // implementation of the icomparable Interface
}??
??????? Interfaces can be inherited. One interface can be inherited from multiple interfaces. Directly or indirectly inheriting itself will cause compilation errors. The following is an example of interface inheritance:
Interface iControl
{
??? Void paint ();
}
Interface itextbox: iControl
{
??? Void settext (string text );
}
Interface ilistbox: iControl
{
??? Void setitems (string [] items );
}
Interface icombobox: itextbox, ilistbox {}????
??????? Through inheritance, the icombobox interface has three method contracts: paint, settext, and setitems. Multi-interface inheritance may cause conflicts between members of the same name. C # solves this problem through clear transformation. See the following example:
Interface ilist
{
??? Int count {Get; set ;}
}
Interface icounter
{
??? Void count (int I );
}
Interface ilistcounter: ilist, icounter {}
Class C
{
??? Void test (ilistcounter X ){
?????? X. Count (1 );???????????????? // Error! Name Conflict
?????? X. Count = 1 ;??????????????? // Error! Name Conflict
?????? (Ilist) x). Count = 1 ;????????? // Correct! Call ilist. Count. Set
?????? (Icounter) x). Count (1 );?????? // Correct! Call icounter. Count
???}
}
??????? Note that this clear transformation is implemented during compilation and will not cause any program running cost. The above example uses the ilistcounter interface as the parameter type of the method test, but we know that the interface cannot be instantiated. How should we pass such a parameter? The answer is to pass an object instance that directly or indirectly implements the class or structure of this interface.
Inheritance
??????? Inheritance is one of the three main characteristics of object-oriented (the other two are encapsulation and polymorphism) and has a considerable application in component programming. C # provides two inheritance mechanisms: class inheritance and interface inheritance. Class inheritance only allows each class to have one parent class (that is, single inheritance), while interface inheritance allows one class or interface to inherit (also called implementation) multiple interfaces at the same time. Classes cannot be inherited by interfaces.
In C #, class inheritance refers to inheriting sub-classes to have all members except the instance constructor, static constructor, and destructor of the parent class. Note that "ownership" and "visibility" are two concepts. A member exists in this class. However, if the member's protection level does not allow the member to be visible in the inherited subclass (such as private or internal ), we will not be able to operate on them in the subclass -- but this does not mean they do not exist. The following example clearly illustrates this point:
Class
{
???? Int count;
???? Public int count
???? {
???????? Get {return count ;}
???????? Set {COUNT = value ;}
????}
}
Class B:
{}
Class Test
{
???? Public static void main ()
???? {
???????? B = new B ();
???????? B. Count = 39;
???????? System. Console. writeline (B. Count );
????}
}
??????? The Count variable is private in a and invisible in B. However, we can clearly feel its existence through the access of the public attribute count.
A subclass can be inherited and a new class member can be added. The New Keyword allows the subclass to block parent class members with the same name during inheritance. Note that the blocking here also means "invisible" rather than "Removing ".
We can use the class modifier keywords abstract and sealed to control the behavior during class inheritance. Abstract: This class can only be inherited by the quilt class, but cannot be instantiated. Sealed does not allow the class to be inherited, so that the inheritance Tree "Ends here ".
Interface inheritance because all the Members are public, its behavior is relatively simple. It only inherits the members of all interfaces. Similar to class inheritance, the New Keyword can also block members with the same name.
Polymorphism
??????? Polymorphism refers to providing different implementation capabilities for methods with the same name, so that we do not need to care about the specific implementation of the method but rely solely on its name for calling operations. For example, we now have a road class that needs to call the drive method, no matter what kind of car we have, bike, car, Jeep, even though their drive method is different, as long as they have the drive method, our road can call their drive method to "Drive" them above ". Polymorphism embodies the common behavior of the object world. C # provides three types of polymorphism capabilities: interface polymorphism, inheritance polymorphism, and abstract polymorphism.
??????? The interface defines the methods, attributes, indexes, and events to be implemented for a type, including possible parameter types and return value types, and submits the specific implementation to the corresponding class or structure, to provide components with polymorphism. Interface polymorphism not only provides a better way to aggregate logical functions for components, but also provides good support for multiple versions of components. As the scale of components continues to grow, more and more members are needed. Simply encapsulating all members in a component does not conform to our logic thinking mode, but also brings about annoying version problems. These members are divided into multiple aggregated logical blocks and encapsulated by Using Interfaces. This method solves the two problems of component design and maintenance. Interface polymorphism is widely used in modern software development.
??????? Class inheritance enables the subclass to automatically have the majority of members of the parent class. C # allows us to provide the component with polymorphism through normal class inheritance. Inheritance is commonly used for Function Extension Based on an existing parent class. It is often implemented by extracting the same members from several classes and placing them in the parent class, then inherit from the Child classes. "Subclass is a parent class, Apple is a fruit" is a classic test term that inherits polymorphism. Inheritance is helpful for small-scale software development. However, in the modern cluster component development mode, it is often not recommended because of obscure logic misuse and unnecessary coding burden.
??????? Abstract polymorphism refers to the multi-state function of inheritance and interfaces through abstract classes. Abstract classes can include both implemented members and only provide member interfaces without specific implementation. Abstract classes cannot be instantiated and must implement corresponding interfaces in their subsequent classes. Otherwise, subclasses should also be marked as abstract. Abstract classes provide a constant function set for their child classes, while also providing them with an elastic interface with scalability. Abstract classes are very useful in the initial design of components. However, because classes cannot be inherited more often, their polymorphism is inferior to interface polymorphism.