[英]Why isn't the compiler throwing an error when there are multiple return statements in the method?
我有一個這樣的 Java 程序-
abstract class Bear{
protected int sing;
protected abstract int grunt();
int sing(){
return sing;
}
}
public class PolarBear extends Bear{
@Override
public int grunt(){
sing() += 10;
return super.grunt();
sing() += 10;
return 10;
return 99;
return 11;
sing() += 10;
}
}
我現在在調用 sing() 方法的任何地方都會出現編譯器錯誤(出現紅色波浪線),因為顯然我將值分配給了一個方法而不是變量,並且我正在調用抽象 grunt 方法。
話雖如此,為什么返回語句不會出現編譯器錯誤? 顯然,我使用了不止一個 return 語句,即使只允許使用一個。 為什么編譯器不會為返回語句拋出錯誤?
我正在使用 Intelij Idea IDE,這是 Jeanne-Boyarsky OCA 題庫中的一個問題。
這是因為之前還有其他錯誤。 在第 13 行,您應該將結果分配給一個變量。 在第 14 行,您正在調用抽象父方法,刪除“超級”以調用您在子中實現的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.