java observer update,Java Observer Update功能

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?

解决方案

In case anybody else is experiencing difficulty in figuring out how to send that second parameter, it is as Nick points out: In the notifyObservers method call.

In the 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.

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);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值