C # sharp experience tenth lecture, inheritance and polymorphism

xiaoxiao2021-03-06  69

Tenth lecture interface inheritance and polymorphism

Interface ??????? Interface define the contract of the object member, is an indispensable part of modern component programming. C # uses keyword "interface" to create an interface. The interface is used as a type, which also has the five access modifications and new redefine modifiers that have other types. The interface can contain four members, attributes, events, and indexers. The interface itself can only declare these members, do not have to provide specific implementation of these members. C # does not allow us to do any access modification for interface members, the access to the interface member is the public - the interface is originally a member contract we defined for other types, and the access limit is not in line with our application interface. The original intention. The same reason, C # also does not allow "Abstract, Virtual, Override, Static" modified to interfaces. Below is a typical example of the 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 Changd; // event ??? string this [int index] {get; set;} // indexer} ????????? Note that we write the interface name into istringList, where The uppercase letter "I" is just a naming agreement of the .NET platform, not necessary. ??????? Interface As a type for "contract", it is not possible to instantiate the class. We say that a data is an interface type, which means that this type implements an interface. C # medium or structure can implement multiple interfaces, and implement the type of interface must provide the implementation of the corresponding interface member. Interface iCloneable {??? Object Clone ();} interface iComparable {??? int compareto (Object Other);} Class Listentry: iCloneable, IComparable {??? public object clone () {...} // iCloneable interface Realization ??? public int compareto (Object Other) {...} // iComparable interface implementation} ????????? interface can inherit, one interface can inherit from multiple interfaces. Direct or indirect inheritance will cause errors when compiling. Below is an example of an interface inheritance: interface icontrol {??? Void Paint ();} interface itextbox: icontrol {??? void settext (string text);} interface ilistbox: icontrol {??? void setItems (String [] Items);} interface ICOBOBOX: ITEXTBOX, IListBox {} ??????????? By inheritance, interface ICOMBOBOX has three method contracts of Paint, setText, setItems. Multi-interface inheritance may bring problems with conflicts with the same name, C # to solve this problem by clear transformation.

Look at the 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); ????????????????? 错 错! Name conflict ?????? x.count = 1; ???????????????? 错 错! Name conflict ?????? (ilist) x) .count = 1; ????????? // correct! Call ilist.count.set ?????? ((iCOUNTER) x) .count (1); ?????? // correct! Call iCOUNTER.COUNT ???}} ??????? Note that this clarity transformation is carried out at compile, and does not bring the price of any program operation. The above example uses the IListCounter interface as the parameter type of TEST, but we know that the interface cannot be instantiated, how should we pass such parameters? The answer is an object instance that passes the class or structure of the interface directly or indirectly. Inheritance ??????? Inheritance is one of the three major features of the object (the other two is packaged and polymorphic), which has a considerable application in component programming. C # provides us with two inheritance mechanisms - class inheritance and interface inheritance. Class inheritance only allows each class to have a parent class (ie, single inheritance), but the interface inherits allows one class or interface to inherit (also known as implementation) multiple interfaces. Class cannot be inherited by the interface. In C # medium inheritance refers to all members of an instance constructor, a static constructor, and a destructor other than the parent class by inheritance. Note that "owned" and "visibility" here are two concepts. Having a member means that the member does exactly in this class, but if the member's protection level does not allow the member to be visible in the inheritance subclass, we will not be able to be in the subclass. Operation - but this does not mean that they don't exist. The following example clearly illustrates this: Class A {???? Int count; ???? public int count ???? {??????? get {return count;} ???? ???? set {count = value;} ????}} Class B: a {} class test {???? public static void main () ???? {???????? b B = new b (); ???????? b.count = 39; ???????? system.console.writeline (b.count); ????}} ???? ??? COUNT variables are private in A, which is not visible in B, but through the access of public attribute count, we can clearly feel its existence. Subclasses can add new class members while inheriting. The new keyword can make the subclass when it is inherited, and it is the same "invisible", not "removal", not "removal". We can use classes to modify keywords Abstract and SeaD to control the behavior of the class inheritance. Abstract This class can only be inherited by subclasses without being instantiated. Sealed does not allow this class to be inherited, so that the tree "here" is inherited. Interface Inherit Because all members are public, its behavior is relatively simple, just a member of all interfaces. The same place inheritance is the same as the NEW key, can also shield the same name member. Polymorphism ?????? Polygonity refers to the ability to provide different implementations for the same name, which makes us only rely on their names without the specific implementation of the method.

转载请注明原文地址:https://www.9cbs.com/read-120000.html

New Post(0)