20.明白接口实现和虚函数实现的区别
接口实现没有多态功能, 以接口调用接口方法,其性质表现为实现接口的类的方法行为。
若要让接口实现多态功能,需要用virtual 实现。如下:
public interface IA
{
void Print();
}
public class MyClass:IA
{
public virtual void Print()
{
Console.WriteLine("MyClass");
}
}
public class MyDerive : MyClass
{
public override void Print()
{
Console.WriteLine("MyDerive");
}
}
static void Main(string[] args)
{
//Program.print();
MyDerive myderve = new MyDerive();
myderve.Print();
IA ia = myderve as IA;
ia.Print();
MyClass myclass = myderve as MyClass;
myclass.Print();
}