2009-10-28 68 views
0

修訂賦值函數方法定義的編寫賦值函數方法

寫賦值函數方法setAge(),它int類型的單個參數,並將 可變時代的價值在這裏你的答案貼:

public int setAge(int age) 
{ 
    return age; 
} 

評論:

* Test 1 (0.0 out of 1) 

     The compilation was successful 
     The output should have been: 
      setAge() Correct 

     This is what was actually produced: 
      setAge() not Correct 

困惑,爲什麼我得到這個錯誤,是不是因爲我setAge後有(INT年齡),這就是爲什麼錯誤了正在添加?

回答

2

嘗試用

public void setAge(int age) 
{ 
    this.age = age; 
} 
+0

雖然我們不能確定該字段的名稱實際上是「年齡」 – 2009-10-28 21:58:53

+0

完美地工作,謝謝 – Tical 2009-10-28 21:59:23

4

您的突變基因實際上並沒有進行任何設置。

我假設你已經有了一段你必須修改的代碼,在這段代碼中搜索變量/字段'age'。

1

您的代碼不會「設置可變年齡的值」。您的方法只返回傳遞給它的值。

0

假設你有一個名爲「時代」一類變量,你可以創建賦值函數類方法,如:

 
public class myTest{ 

public int age; 

//other code here..if any 

public void setAge(int age) 
{ 
    this.age = age; 
} 

//other code here.. if any 
} 

通常你setAge方法不應返回任何東西。 Mutator只修改值。

要返回值,應該使用名爲'Accessor'的getAge()方法。