10.3 接口的实现
在继续前,先讨论一下如何定义和实现接口。第9章介绍了接口定义的方式与类相似,使用的代码如下:
interface IMyInterface {
// Interface members.
}
但有几个重要的区别:
不允许使用访问修饰符(public、private、protected或internal),所有的接口成员都是公共的。
接口成员不能包含代码体。
接口不能定义字段成员。
接口成员不能用关键字static、virtual、abstract或sealed来定义。
类型定义成员是禁止的。
但要隐藏继承了基接口的成员,可以用关键字new来定义它们,例如:
interface IMyBaseInterface {
void DoSomething();
}
interface IMyDerivedInterface : IMyBaseInterface {
new void DoSomething();
}
其执行方式与隐藏继承的类成员的方式一样。
在接口中定义的属性可以定义访问块 get 和 set 中的哪一个能用于该属性(或将它们同时用于该属性),例如:
interface IMyInterface {
int MyInt( get; set; )
}
其中int属性MyInt有get和set存取器。对于访问级别有更严限制的属性来说,可以省略它们中的任一个。
note : 这个语法类似于自动属性,但自动属性是为类(而不是接口)定义的,自动属性必须包含get和set存取器。
最后,接口与类一样,可以定义为类的成员(但不能定义为其他接口的成员,因为接口不能包含类型定义)。
在类中实现接口
现接口的类必须包含该接口所有成员的实现代码,且必须匹配指定的签名(包括匹配指定的get和set块),并且必须是公共的。例如:
public interface IMyInterface {
void DoSomething();
void DoSomethingElse();
}
public class MyClass : IMyInterface {
public void DoSomething() {
}
public void DoSomethingElse() {
}
}
还可以在基类上实现接口成员,例如:
public interface IMyInterface {
void DoSomething();
void DoSomethingElse();
}
public class MyBaseClass { // 注意这里的基类并没有继承接口
public void DoSomething() {
}
}
public class MyDerivedClass : MyBaseClass, IMyInterface {
// DoSometing()接口在基类中实现
public void DoSomethingElse() {
}
}
继承一个实现给定接口的基类,就意味着派生类隐式地支持这个接口,例如:
public interface IMyInterface {
void DoSomething();
void DoSomethingElse();
}
public class MyBaseClass : IMyInterface {
public virtual void DoSomething() {
}
public virtual void DoSomethingElse() {
}
}
public class MyDerivedClass : MyBaseClass {
public override void DoSomething() {
}
}
如果要使用new关键字隐藏一个基类成员,而不是重写它,则方法IMyInterface.DoSomething()就总是引用基类版本,即使通过这个接口来访问派生类,也是这样。
1. 显示实现接口成员
类显式地实现接口成员。如果这么做,该成员就只能通过接口来访问,不能通过类来访问。上一节的代码中使用的隐式成员可以通过类和接口来访问。
隐式地实现接口IMyInterface的方法DoSomething(),如上所述,则下面的代码就是有效的:
MyClass myObj = new MyClass();
myObj.DoSomething();
下面的代码也是有效的:
MyClass myObj = new MyClass();
IMyInterface myInt = myObj;
myInt.DoSomething();
显式实现DoSomething(),就只能使用后一种技术(只能用接口访问)。其代码如下:
public class MyClass : IMyInterface {
void IMyInterface.DoSomething() { // 显示实现
}
public void DoSomethingElse() {
}
}
其中 DoSomething()是显式实现的,而 DoSomethingElse()是隐式实现的。只有后者可以直接通过MyClass的对象实例来访问。