使用接口引用变量调用基类方法

Sha*_*run 1 c# inheritance interface

我有一个基类和一个接口.现在我正在创建一个子类.如果我创建一个接口类型的引用变量来指向子类的对象,我可以使用它来访问基类方法吗?

Class BaseClass
{
public void baseClassMethod()
{
.....
}
}  
Interface MyInterface
{
public void Interfacemethod();
}


Class ChildClass:BaseClass, MyInterface
{
....

}


....


main()
{
MyInterface myclass= new ChildClass ();
myclass.baseClassMethod();//Is this possible? y?
 }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 6

该变量myclass是静态类型的MyInterface,它没有一个名为baseClassMethod()- 所以没有的方法,它不起作用.您需要将引用转换回BaseClasssChild(或者可以正常),或者将方法添加到MyInterface(或一些其他接口).