[英]Calling overloaded functions from child class
我有2個非常相似的課程。 假設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
}
}
我希望能夠同時擁有Birds和Hawks。 當在Hawk上調用fly()時,我仍然想運行Bird的fly方法,最后只進行一些更改。 我要這樣做正確嗎?
嘗試使用super
關鍵字,例如:
class Hawk extends Bird {
public void fly() {
super.fly() // use keyword super instead of parent
}
}
使用超級關鍵字。 當我們要調用超類方法/構造函數時,我們只使用Super關鍵字
public void fly()
{
Super.fly()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.