我有两个非常相似的类(class)。假设bird
和hawk
class Bird
{
...
public void fly()
{
//do a lot of stuff here
}
}
class Hawk extends Bird
{
public void fly()
{
parent.fly() // how can I call the overloaded parent method?
//a couple changes specific to hawk here
}
}
我希望能够同时拥有小鸟和老鹰。当在 Hawk 上调用 Fly() 时,我仍然想运行 Bird 的 Fly 方法,只是最后进行了一些更改。我以正确的方式处理这件事吗?
最佳答案
尝试使用 super
关键字,例如:
class Hawk extends Bird {
public void fly() {
super.fly() // use keyword super instead of parent
}
}
关于java - 从子类调用重载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555340/