显式接口: 同一个类里有2个或2个以上的具有相同方法的interface,这时候要用显式接口
void IWindow.Close() //显性显式接口 是不可见的,默认是私有的
{....}
void IFile.Close()
{...}
public void Close()
{
//转化
(this as IFile).Close(); //this在这里是当对象的实例.
((IWindow)this).Close();
//以上两种转化接口的效果是一样的.
}
不允许使用访问修饰符来实现显示接口. 因此,为了访问这些成员,必须把相关对象转换为相应的接口类型,
Chimpanzee chimp = new Chimpanzee();
IHerbivore vchimp = (IHerbivore) chimp;
bool hungry = vchimp.IsHungry;
No comments:
Post a Comment