1

我们知道,在 Java 中,当一个方法返回一个值时,我们必须将该值存储在该类型的变量中。

例如,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

3 回答 3

4

我们知道,在 Java 中,当一个方法返回一个值时,我们必须将该值存储在该类型的变量中。

这句话的每一部分都是错误的。
你应该买一本更好的书。

于 2012-03-18T05:51:47.327 回答
3

ChoiceGroupappend方法返回元素的指定索引。如果您不打算使用它,可以忽略返回值。

方法签名 - 返回值和参数在API 文档中有明确定义的含义:

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
于 2012-03-18T15:16:36.260 回答
2

如果你想使用返回值,那么你存储在一个变量或对象中。如果你不想要,那就离开它。这在 java 中不是一个错误

于 2012-04-14T12:07:07.727 回答