2012-03-18 43 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中的错误

相关问题