《C#高级编程》里面如是说:接口IJumper{......}类 Class MyJumper:IJumper{......}IJumper ITest;
MyJumper JumperTest = new MyJumper();ITest = JuperTest;书上说ITest可以执行JuperTest中MyJumper中定义而IJuper中没有定义的公共方法,但是我试了一下,如果不类型转换,ITest此时不能执行MyJumper中定义而IJuper中没有定义的公共方法,与书上所说不一致,这是什么原因呢?
MyJumper JumperTest = new MyJumper();ITest = JuperTest;书上说ITest可以执行JuperTest中MyJumper中定义而IJuper中没有定义的公共方法,但是我试了一下,如果不类型转换,ITest此时不能执行MyJumper中定义而IJuper中没有定义的公共方法,与书上所说不一致,这是什么原因呢?
不进行类型转换不能调用MyJumper中定义而IJumper中没有定义的公共方法
我写了下面的程序来验证,如果不转换的话,不要说执行了,编译也无法通过:namespace MyTtest
{
interface IJumper
{
void a();
} class MyJumper : IJumper
{
void IJumper.a()
{
Console.WriteLine("a");
}
public void b()
{
Console.WriteLine("b");
}
} class a
{
static void Main()
{
IJumper ITest;
MyJumper JumperTest = new MyJumper(); ITest = JumperTest; //ITest.b(); compiletime error
((MyJumper)ITest).b();
}
}
}
{
void MethodA();
void MethodB();
}MSDN中的文字。
链接:ms-help://MS.MSDNQTR.2003FEB.2052/csref/html/vcrefTheInterfaceType.htm
版本:VS.NET 2003 MSDN
谁说接口不可以被继承了?
--------------------------谁又说接口 可以被继承了?
sorry,上下文没说清楚,看看楼主原文。