java Java观察者更新功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13901339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Observer Update function
提问by TheNotMe
I have a class that implements observer, and of course it needs to have the update function:
我有一个实现观察者的类,当然它需要具有更新功能:
public void update(Observable obs, Object obj);
Can someone please explain what do the two parameters stand for? Observable is my observable of course, but, how can I access my observable fields through this Observable obs object? And what is Object obj?
有人可以解释一下这两个参数代表什么吗?Observable 当然是我的 observable,但是,如何通过这个 Observable obs 对象访问我的 observable 字段?什么是Object obj?
回答by micha
obs
is the object that extends Observable
and has the notifyObservers
method. You can cast obs
to your object that extends Observable
and then call the methods you need.
obj
is the optional parameter that can be passed to notifyObservers
.
obs
是扩展Observable
并具有notifyObservers
方法的对象。您可以转换obs
为扩展的对象,Observable
然后调用所需的方法。
obj
是可以传递给 的可选参数notifyObservers
。
回答by Patricia
In case anybody else is experiencing difficulty in figuring out how to send that second parameter, it is as Nick points out: In the notifyObserversmethod call.
如果其他人在弄清楚如何发送第二个参数时遇到困难,正如尼克指出的那样:在notifyObservers方法调用中。
In the Observable:
在 Observable 中:
private void setLicenseValid(boolean licenseValid) {
this.licenseValid = licenseValid;
setChanged(); // update will only get called if this method is called
notifyObservers(licenseValid); // add parameter for 2nd param, else leave blank
}
In the Observer:
在观察者中:
@Override
public void update(Observable obs, Object arg) {
if (obs instanceof QlmLicense) {
setValid((Boolean) arg);
}
}
Be sure to wire up your Observable correctly, else your update method will not get called.
确保正确连接您的 Observable,否则您的更新方法将不会被调用。
public class License implements Observer {
private static QlmLicense innerlicense;
private boolean valid;
private Observable observable;
private static QlmLicense getInnerlicense() {
if (innerlicense == null) {
innerlicense = new QlmLicense();
// This is where we call the method to wire up the Observable.
setObservable(innerlicense);
}
return innerlicense;
}
public boolean isValid() {
return valid;
}
private void setValid(Boolean valid) {
this.valid = valid;
}
// This is where we wire up the Observable.
private void setObservable(Observable observable) {
this.observable = observable;
this.observable.addObserver(this);
}
@Override
public void update(Observable obs, Object arg) {
if (obs instanceof InnerIDQlmLicense) {
setValid((Boolean) arg);
}
}
}
回答by Nick Robertson
The observer's update(Observable obs,Object obj) method receives through the notifyObservers the object(second parameter) who has changed(in the Observable).
观察者的update(Observable obs,Object obj) 方法通过notifyObservers 接收已经改变的对象(第二个参数)(在Observable 中)。