2016-02-27 75 views
0

这是我在父类@覆盖Java的父类

public boolean ChoiceOfItem(){ 
    if (bread) 
     this.Choice("bread"); 
    if (meat) 
     this.Choice("meat"); 
    if (lettuce) 
     this.Choice("lettuce"); 
    if (tomato) 
     this.Choice("tomato"); 
    if (carrot) 
     this.Choice("carrot"); 
    return false; 
} 

代码,这一个是从扩展(不父)类:

@Override 
public boolean ChoiceOfItem() { 
    if (ryeBread) 
     this.Choice("ryeBread"); 
    return false; 
} 

我的问题是什么错在哪里? 期待您的消息。如果有必要,我将能够发送整个代码。

+6

那么......“什么是错的” - 你告诉我们!你的问题是什么? – Marvin

+3

你应该在扩展类中调用'return super.ChoiceOfItem()'而不仅仅是false –

+2

你应该编辑这个问题并至少提供:1)代码的预期结果是什么(你想要达到什么目的? )2)代码的实际结果是什么(会发生什么,为什么会出错?) 否则,您可能会在问题关闭时结束。 –

回答

0

ChoiceOfItem在处理某个项目时可能需要返回true,因此仅在该情况下才返回false。

public boolean choiceOfItem(){ 
    if (bread) 
     this.Choice("bread"); 
    else if (meat) 
     this.Choice("meat"); 
    else if (lettuce) 
     this.Choice("lettuce"); 
    else if (tomato) 
     this.Choice("tomato"); 
    else if (carrot) 
     this.Choice("carrot"); 
    else return false; 
    return true; 
} 

@Override 
public boolean choiceOfItem() { 
    if (super.choiceOfItem()) 
     return true; 
    if (ryeBread) { 
     this.Choice("ryeBread"); 
     return true; 
    } 
    return false; 
} 
+0

我认为我通过更改代码如下来对它进行整理 '@Override public boolean ChoiceOfItem(){ super.ChoiceOfItem(); if(ryeBread){ this.Choice(“ryeBread”); 返回true; } return false; }' 感谢您的建议P.S:对不起,我不能让它清晰 – TheSarcasticDiode

0

首先要说那些方法一直都会返回false。 @Override只是一个标志,告诉编译器该方法被覆盖。例如,当您更改父类的方法签名时,这是很有意义的,因此编译器会在子类中告诉您您不再覆盖这里。

+0

感谢它帮助 – TheSarcasticDiode