将更新的超类值传递给子类

时间:2015-12-02 01:12:18

标签: java

我试图将超类中已更新的值传递给子类这可能吗?

例如我有:

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"
    }
}

我该怎么做?

2 个答案:

答案 0 :(得分:0)

怎么样

public anotherClass extends class1{

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

答案 1 :(得分:0)

在您的示例中,您覆盖method中的class1。通常,我们编写如下代码:

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

如果您尝试在class中调用方法的同时调用anotherClass中定义的方法,请使用super关键字调用它:< / p>

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