C # graphic tutorials study notes - Interface

First, the concept of the interface
the interface is designated a set of functions to achieve their members without reference types . So only classes and structures to implement the interface.

 

Second, the interface is declared
1. interface declaration does not contain: data members, static members can only contain the following types of non-static member function: methods, properties, events, indexers.
2. The interface function member declaration does not contain any implementation code, only with a semicolon.
3. By convention, interface names begin with a capital letter I (Interface).
4. Like the class structure and interface declarations may division.
The interface declaration can have any access modifiers public, protected, internal, or private. Members of an interface are implicitly public, it does not allow any access modifiers, including the public.
Example:

 

Third, implement the interface
only classes and structures to implement interfaces:
1. If a class implements an interface, it must implement all the members of the interface.
2. If a class implements an interface and inherits from the base class, the base class list of base class name must be placed before all interfaces.
Example:

 

Fourth, the use of the interface
1. The object reference point to obtain a reference interface:
(1) cast: If the class does not implement a corresponding interface, an exception is thrown.
Example:

(2) use as operator: if a class does not implement a corresponding interface, returns null.
Example:

2. 类或结构可以实现多个接口:
如果一个类实现了多接口,并且其中有些接口有相同签名和返回类型,这时,类可以实现单个成员来满足所有包含重复成员的接口,也可以为每个接口分离实现,即创建显式接口成员实现。
示例:

3. 如果类实现了多接口,我们可以获取每个接口的独立引用。
4. 实现接口的类可以从它的基类继承实现的代码。
5. 接口可以继承接口。

Guess you like

Origin www.cnblogs.com/wujuntian/p/10990355.html