2015-12-02 33 views
0

我想將超類中已更新的值傳遞給子類,這可能嗎?將更新的超類值傳遞給子類

比如我有:

public class1{ 
    @FXML 
    ListView <String> list; 

    // then list gets changed in a method 

    public void method(){ 
    list.getItems().add("hello"); 
    } 
} 

而且在不同的類:

public anotherClass extends class1{ 

    public void method(){ 
    System.out.println(list); 
    // here it seems that the list has not been updated and I get a null value when printing it instead of a list filled with "hello" 
    } 
} 

我怎樣才能做到這一點?

回答

0

如何

public anotherClass extends class1{ 

    public void method(){ 
     super.method(); 
     System.out.println(list); 
    } 
} 
0

在你的榜樣,您覆蓋class1method。一般情況下,我們寫這樣的代碼是這樣的:

class anotherClass extends class1 
{ 
    @Override // Since you replace the method in class1 
    public void method() 
    { 
     // do something 

如果你想打電話給在同一時間,當你調用方法anotherClassclass定義的方法,把它用super關鍵字:

public void method() 
{ 
    super.method(); 
    // do something else!