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();