2012-03-18 91 views
1

我們知道,在Java中,當一個方法返回一個值時,我們必須將該值存儲在該類型的變量中。J2ME的append()方法

例如,getString()返回String,並將該值存儲在String變量中。

在J2ME中,我試圖創建無線電按鈕,即使用ChoiceGroup類。

ChoiceGroup radio; 
radio=new ChoiceGroup("Select your Color",Choice.EXCLUSIVE); 
radio.append("Red",null); 
radio.append("White",null); 
radio.append("Green",null); 

在書的append()方法的簽名是

int append(String string, Image img) 

我想問的是,即使我不是存儲從append()方法返回的整數值我的代碼運行完美。

我使用無線工具包2.5.2

注意這本書並沒有給予這方面的任何原因,這就是爲什麼我在這裏問。

回答

4

我們知道在Java中,當方法返回一個值時,我們必須將該值存儲在該類型的變量中。

該句子的每個部分都是錯誤的。
你應該得到一本更好的書。

+0

你的意思是說在調用方法getString();這是由程序員來存儲返回的字符串? – 2012-03-18 05:54:17

+1

@ R122:是的。對於像'getString()'這樣的方法,除了返回一個值之外什麼都不做,你可能會想要存儲返回值(否則,調用將是無用的)。對於像append()這樣的改變內容的方法,通常沒有理由存儲返回的值(除非你真的需要它)。 – SLaks 2012-03-18 05:55:48

3

ChoiceGroup append方法返回元素的分配索引。如果你不打算使用它,可以忽略返回的值。

方法簽名 - 返回值和參數都在API documentation意思明確規定:

public int append(String stringPart, 
       Image imagePart) 

Appends an element to the ChoiceGroup. 

Specified by: 
    append in interface Choice 

Parameters: 
    stringPart - the string part of the element to be added 
    imagePart - the image part of the element to be added, 
       or null if there is no image part 
Returns: 
    the assigned index of the element 
Throws: 
    NullPointerException - if stringPart is null 
2

如果你想使用的返回值,那麼你在一個變量存儲或object.If你不想再離開它。它不是在java中的錯誤

相關問題