接口引用如何能够调用子类方法。
在下面的示例中,接口引用如何访问测试类对象?
interface ITest
{
int add();
}
public class Test : ITest
{
public int add()
{
return 1;
}
public int sub()
{
return -1;
}
}
static void Main(string[] args)
{
ITest t = new Test();
Console.WriteLine((t as Test).sub());
}
Run Code Online (Sandbox Code Playgroud)
-1。