2014-10-16 68 views
1

我正在學習java的過程。java圍繞類的括號存儲引用按鈕

我從Java引用這段代碼: -

public class ButtonList extends Applet implements ActionListener { 
String msg = ""; 
Button bList[] = new Button[3]; 
public void init() { 
    Button yes = new Button("Yes"); 
    Button no = new Button("No"); 
    Button maybe = new Button("Undecided"); 
    // store references to buttons as added 

    bList[0] = (Button) add(yes); <<<<<<<<<<<<<<<<<<<<<<<<<<<<< here 

    bList[1] = (Button) add(no); 
    bList[2] = (Button) add(maybe); 
    // register to receive action events 
    for(int i = 0; i < 3; i++) { 
     bList[i].addActionListener(this); 
    } 
} 

而且我不知道什麼是 「(按鈕)添加(是)」 表示。

如果在前面的 段落中聲明爲按鈕,爲什麼bList []應重新指定爲Button。

還有什麼Button whithin括號在這裏代表?

回答

1

(Button) myVar插入一個運行時檢查是myvar被分配一個Button,以便編譯器可以安全地假設myVarButton。如果myVar在運行時不是Button,則會引發異常。

請參閱明確鑄造以獲取更多信息