[英]Getting value of observed object?
How do I get the value of watched
which is "New Value"? 我如何获得被
watched
的价值,即“新价值”?
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");
}
}
Output: 输出:
setValue method called...
Value changed
Line 40: 40行:
System.out.println("Value changed" + new value of watched!); // that's what I want.
Wanted Output: 想要的输出:
setValue method called...
Value changed to New Value
You already have a setter method in the form of setValue
so now just implement a corresponding getter method: 您已经具有
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");
}
}
Output: 输出:
setValue method called...
Value changed: New Value
You can access the new value from the Observable by providing a getter function like given below and call the notifyObservers(); 您可以通过提供如下所示的getter函数从Observable访问新值,然后调用notifyObservers();。 inside the
setValue(String value)
在
setValue(String value)
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 ;
}
}
And modify the below method to get the new value 并修改以下方法以获取新值
public void update(Observable obj, Object arg) {
System.out.println("Update called :"+obj.getValue());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.