technically it seems OK to define Class as Observable and Observer using the following code:
public class Data extends Observable implements Observer
however, trying to implement it, it does not work.
public class Data extends Observable implements Observer { @Override public void update(Observable o, Object o1) { System.out.println("SC"); } Integer A; String B; Float C; public Data() { this.addObserver(this); } public void setA(Integer A) { this.A = A; notifyObservers(); } public void setB(String B) { this.B = B; notifyObservers(); } public void setC(Float C) { this.C = C; notifyObservers(this.C); } }
using the main function as shown below:
public static void main(String[] args) { Data d = new Data(); d.setA(5); d.setB("Hi"); d.setC(2.0f); }
it should print some "SC", but it does not work. Can someone explain why?
source share