Note: If a class implements an interface, the class must implement the method used by the interface and its parent interface unless the class is an abstract class that is implemented by subclasses of that class! An interface can also inherit from another interface without implementing the interface's methods but instead to its implementation
Demo:
1 //Interface One2 PublicInterface IEat3{4 voidEat ();5}6 //Interface two7 PublicInterface Ipaly8{9 voidPlay ();Ten} One //Interface three A PublicInterface Itravel:ieat,ipaly -{ - //Interface three inherit interface one and interface two the} - - Public classProgram:itravel -{ + Public voidEat () -{ + //Omit method body A} at Public voidPlay () -{ - //Omit method body -} -}Mode
From the demo above, some of the ieat interfaces and Ipaly interfaces that can be seen in the Itravel interface may have some friends asking "Do you not support multiple inheritance in C # and Java?"
Note that this is for classes, but for interfaces a class is the same interface that can implement multiple interfaces and can inherit multiple interfaces
The difference between an interface and an abstract class:
When there is a parent-child class relationship between the objects that you need, consider using an abstract class.
When there are no inheritance relationships between objects, only the same ability, consider using interfaces
Abstract class is the abstract result of a certain kind of thing, and the interface is a kind of ability to represent certain things.
Note Points for the interface:
01. Interfaces exist to constrain the format of the method (parameters and return value types)
02. Interfaces can implement multiple inheritance to compensate for the defects of single inheritance.
03. Interface can be regarded as a special abstract class, through the anti-compilation to see the source
04. The method in the interface does not have access modifiers, because the CLR is automatically added and cannot have a method body
05. If a class implements an interface, it has to implement all the methods in that interface
06. The interface should be used with caution to prevent the interface pollution!
07. The interface represents only one capability, and the classes and interfaces that implement the interface do not have an inheritance relationship
08. The interface is used for implementation, and the class is used for inheritance.
09. In fact, many times, seemingly can not interface, because the interface is a method of the Convention,
10. An abstract class can also implement an interface, but an interface member must map to an abstract member of an abstract class. Derived classes of abstract classes if they are non-abstract classes, you must implement interface members through method overloads
Indexer
From the above code we can see that the GetEnumerator () method of the IEnumerable interface returns an object that implements the IEnumerator interface!
1 //Abstract:2 //supports simple iterations of non-generic collections. 3[ComVisible (true)]4[Guid ("496b0abf-cdee-11d3-88e8-00902754c43a")]5 Public InterfaceIEnumerator6{7 //Abstract:8 //Gets the current element in the collection. 9 //Ten //Return Result: One the current element in the//collection. A ObjectCurrent {Get; } - - //Abstract: the //Pushes the enumerator to the next element of the collection. - // - //Return Result: - True if the enumerator is successfully pushed to the next element, or False if the enumerator crosses the end of the collection. + // - //Exceptions: + //System.InvalidOperationException: A //The collection was modified after the enumerator was created. at BOOLMoveNext (); - // - //Abstract: - //sets the enumerator to its initial position, which precedes the first element in the collection. - // - //Exceptions: in //System.InvalidOperationException: - //The collection was modified after the enumerator was created. to voidReset (); +}IEnumerator prototypes
When we use foreach to traverse an object, the Getienumerable method of the formation is executed first to get an iterator (the object that implements the IEnumerator interface), and then call the Reset method of the object to initialize the enumerator, Then execute the object's MoveNext () method to get the bool value if the method returns true to call the object's current property to get the value in the Execute MoveNext () method until the method returns false to exit the iteration
Demo:
1 //classes available for foreach traversal (implements the Ienmerable interface)2 Public classMylist:ienumerable3{4ArrayList list =NewArrayList ();5 Public voidADD (ObjectO6{7List. ADD (o);8}9 PublicIEnumerator GetEnumerator ()Ten{ One return NewMyienumerator (list); A} -} - //Implements the class of the IEnumerator interface the Public classMyienumerator:ienumerator -{ - PublicMyienumerator (ArrayList list) -{ + This. list = list; -} +ArrayList list =NewArrayList (); A intI =-1; at Public ObjectCurrent -{ - Get{returnList[i]; } -} - - Public BOOLMoveNext () in{ - BOOLFalg =false; to if(I<list. COUNT-1) +{ -i++; theFalg =true; *} $ returnFalg;Panax Notoginseng} - the Public voidReset () +{ A the} +}customizing classes that are available for foreach traversal