Observable Notify 多个变量变化

Observable Notify Multiple variables change

如何通知多个变量更新?

有什么方法可以检测和分离notifyObservers吗?

public class AnimalWeightObservable extends Observable {

    public long animalId;

    public long weigh;

    public long getAnimalId() {
        return animalId;
    }

    public AnimalWeightObservable setAnimalId(long animalId) {
        this.animalId = animalId;
        this.setChanged();
        this.notifyObservers(animalId);
        return this;
    }

    public long getWeigh() {
        return weigh;
    }

    public AnimalWeightObservable setWeigh(long weigh) {
        this.weigh = weigh;
        this.setChanged();
        this.notifyObservers(weigh);
        return this;
    }

}

如何检测 变量已更改?

如何将 animalIdweight 包装在另一种类型中:例如 AnimalProperty

class AnimalProperty<T> {
    String propertyName;
    T property;

    AnimalProperty(String name, T property) {
        this.propertyName = name;
        this.property = property;
    }
}

因此您的代码将如下所示:

public class AnimalWeightObservable extends Observable {

    public AnimalProperty animalId;

    public AnimalProperty weigh;

    //...
    //...

    public AnimalWeightObservable setWeigh(AnimalProperty weigh) {
        this.weigh = weigh;
        this.setChanged();
        this.notifyObservers(weigh);
        return this;
    }
}

然后在 Observerupdate(...) 方法中打开 propertyName 以了解更新了哪个 属性