获得观察对象的价值?

时间:2017-01-21 10:47:54

标签: java

如何获得watched的值,即#34;新值"?

来自here的代码:

import java.util.Observable;
import java.util.Observer;

class ObservedObject extends Observable {
   private String watchedValue;

   public ObservedObject(String value) {
   watchedValue = value;
   }

   public void setValue(String value) {

   // if value has changed notify observers
   if(!watchedValue.equals(value)) {
   watchedValue = value;

   // mark as value changed
   setChanged();
   }
   }
}

public class ObservableDemo implements Observer {
   public String name;
   public ObservableDemo(String name) {
   this.name = name;
   }

   public static void main(String[] args) {
   // create watched and watcher objects
   ObservedObject watched = new ObservedObject("Original Value");

   // watcher object listens to object change
   ObservableDemo watcher = new ObservableDemo("Watcher");

   // add observer to the watched object
   watched.addObserver(watcher);

   // trigger value change
   System.out.println("setValue method called...");
   watched.setValue("New Value");

   // check if value has changed
   if(watched.hasChanged())
   System.out.println("Value changed" + new value of watched!); // that's what I want. ("New Value")
   else
   System.out.println("Value not changed");
   }

   public void update(Observable obj, Object arg) {
   System.out.println("Update called");
   }
}

输出:

setValue method called...
Value changed

第40行:

System.out.println("Value changed" + new value of watched!); // that's what I want.

通缉输出:

setValue method called...
Value changed to New Value

2 个答案:

答案 0 :(得分:0)

您已经有setValue形式的setter方法,所以现在只需实现相应的getter方法:

import java.util.Observable;
import java.util.Observer;

class ObservedObject extends Observable {
    private String watchedValue;

    public ObservedObject(String value) {
        this.watchedValue = value;
    }

    public String getWatchedValue() {
        return watchedValue;
    }

    public void setWatchedValue(String value) {
        if (!watchedValue.equals(value)) {
            this.watchedValue = value;
            setChanged();
        }
    }
}

public class ObservableDemo implements Observer {
    public String name;

    public ObservableDemo(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        ObservedObject watched = new ObservedObject("Original Value");
        ObservableDemo watcher = new ObservableDemo("Watcher");
        watched.addObserver(watcher);
        System.out.println("setValue method called...");
        watched.setWatchedValue("New Value");
        if (watched.hasChanged()) {
            System.out.println("Value changed: " + watched.getWatchedValue());
        } else
            System.out.println("Value not changed");
    }

    public void update(Observable obj, Object arg) {
        System.out.println("Update called");
    }
}

输出:

setValue method called...
Value changed: New Value

答案 1 :(得分:0)

您可以通过提供如下所示的getter函数从Observable访问新值,并在setValue(String value)

中调用 notifyObservers();
class ObservedObject extends Observable {
   private String watchedValue;

   public ObservedObject(String value) {
   watchedValue = value;
   }

   public void setValue(String value) {

   // if value has changed notify observers
   if(!watchedValue.equals(value)) {
   watchedValue = value;

   // mark as value changed
   setChanged();
   notifyObservers();
   }
   }
   public String getValue(){
    return watchedValue ;
   }
}

并修改以下方法以获取新值

public void update(Observable obj, Object arg) {
   System.out.println("Update called :"+obj.getValue());
 }