您的位置:首页 > 编程语言 > C#

C#中 多态的作用

2016-06-09 09:00 211 查看
How to use Polymorphism, Upcasting, and Downcasting to create powerful and dynamic functionality
between inherited classes

如何使用多态,向上转型和向下转换创建继承的类之间的强大和动态功能

public new void Chop()
{
Debug.Log("The apple has been chopped.");
}

public new void SayHello()
{
Debug.Log("Hello, I am an apple.");
}

此处new关键字用于区别父类和子类中的同名方法,并不是重写这个方法,没有用的virtual和override关键字,所以并不是重写,只是用于区别父类和子类的同名方法
也可以:

new public void Yell()
{
Debug.Log ("Enemy version of the Yell() method");
}

实现在子类中隐藏父类与子类同名的方法
多态的体现:
ParentsClass
myClass=new ChildClass();
myClass.ParentsMethod();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#多态