- 论坛徽章:
- 0
|
你是练习的,还是需要根据特定代码改的,the following is a simple example
- import java.util.*;
- public class test2 extends Observable
- {
- public test2(){}
- public static void main(String[] xx)
- {
- test2 o = new test2();
- /**
- * adds any objects to be observed
- */
- o.addObserver(new oo1());
- o.addObserver(new oo2());
- /**
- * o.hasChanged() will return true after o.setChanged()
- * and you can call o.clearChanged() to cancle it
- */
- o.setChanged();
- /**
- * notify all of its observers and pass a argument
- */
- o.notifyObservers("changed!");
- /**
- * o.hasChanged() will return false,because it finished the notification
- *
- */
- }
- static class oo1 implements Observer
- {
- public void update(Observable o, Object arg)
- {
- System.out.println("oo1:"+arg);
- }
- }
- static class oo2 implements Observer
- {
- public void update(Observable o, Object arg)
- {
- System.out.println("oo2:"+arg);
- }
- }
- }
复制代码 |
|