2014-04-21 125 views
-2

我被要求重寫一個方法來實現新的行爲,我已經做了構造函數和方法。重寫方法

private String name; 
private boolean disease; 

public Area(Position pos, String name, boolean disease) { 
    super(pos); 

    this.name = name; 
    this.disease = disease; 
} 

public String getName() { 
    return name; 
} 

和方法我要重寫上的一個區域,使疾病得以停止,如果藥是足夠

public boolean hasDisease() { 
    return disease; 
} 

我嘗試使用

if (medicine = true) { 
     disease = false 
    } 
    return disease = true; 
} 

但引起其他測試失敗。

+4

比較:'=='。作業:'='。 –

回答

5

您使用=而不是==來測試是否相等。

if(medicine == true) { // <-- Fixed this. 
    disease = false 
} 
    return disease = true; 
} 

可以解決這個問題更:

if(medicine) 
    disease = false 
return disease; 
0

對答案添加上述

=是賦值運算符,所以說

INT A = B;

a將取b的值。

其中as ==是比較運算符。

因此,a == b實際上是一個函數調用,如果a和b具有相同的值,則返回true的布爾結果,如果不具有相同的值,則返回false。對於像int,double,Boolean,float等基本類型,它會比較值,對於複雜類型(如某些對象的實例)(大多數語言),它會比較變量的內存位置。換言之,

String s = "This is True"; 
if(s == "This is True"){ 
    return true; 
} 
return false; 

將永遠不會返回true,因爲s和「This is True」不會駐留在內存中的相同位置。爲此,您需要使用

if(s.equals("This is True"){ 
    return true; 
} 
return false;