当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.util.Observable用法及代码示例


java.util.Observable用于创建程序其他部分可以观察到的子类。当此类子类的对象发生更改时,观察类会收到通知。这更新( )当观察者收到更改通知时调用该方法。

Note: Observing class must implement the Observer interface, which defines the update( ) method.

被观察的对象必须遵循两个简单的规则:

  1. 如果改变,必须调用设置更改()方法。
  2. 当它准备好通知观察者此更改时,它必须调用通知观察者()方法。这导致更新( )要调用的观察对象中的方法。
Be careful, if the object calls notifyObservers( ) method without having previously called
setChanged( ) method, no action will take place.

被观察对象必须同时调用设置更改()通知观察者()方法,之前更新( )将被调用。
java.util.Observable 的构造函数:

  • 可观察()
    构造一个具有零个观察者的 Observable。

方法:

  1. addObserver(观察者观察者):将观察者添加到观察调用对象的对象列表中。
    Syntax : public void addObserver(Observer observer)
    Exception : NullPointerException -> if the parameter observer is null
    
    
    // Java code to demonstrate addObserver() method 
    import java.util.*; 
      
    // This is the observer 
    class Observer1 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer1 is added"); 
        } 
    } 
      
    // This is class being observed 
    class BeingObserved extends Observable 
    { 
        void incre()  
        { 
            setChanged(); 
            notifyObservers(); 
        } 
    } 
      
    class ObserverDemo { 
        // Driver method of the program 
        public static void main(String args[])  
        { 
            BeingObserved beingObserved = new BeingObserved(); 
            Observer1 observer1 = new Observer1(); 
            beingObserved.addObserver(observer1); 
            beingObserved.incre(); 
        } 
    } 

    输出:

    Observer1 is added
  2. setChanged():当调用对象发生更改时调用。
    Syntax : protected void setChanged( )
    Exception : NA.
    
    
    // Java code to demonstrate setChanged() method 
    import java.util.*; 
      
    // This is first observer 
    class Observer1 implements Observer 
    { 
        public void update(Observable obj, Object arg) { } 
    } 
      
    // This is class being observed 
    class BeingObserved extends Observable 
    { 
        void func1() 
        { 
            setChanged(); 
            System.out.println("Change status with setChanged :" + hasChanged()); 
            notifyObservers(); 
        } 
          
        void func2() 
        { 
            System.out.println("Change status without setChanged :" + hasChanged()); 
            notifyObservers(); 
        } 
    } 
      
    class ObserverDemo { 
        // Driver method of the program 
        public static void main(String args[])  
        { 
            boolean status; 
            BeingObserved beingObserved = new BeingObserved(); 
            Observer1 observer1 = new Observer1(); 
            beingObserved.addObserver(observer1); 
            beingObserved.func1(); 
            beingObserved.func2(); 
        } 
    } 

    输出:

    Change status with setChanged :true
    Change status without setChanged :false
  3. clearChanged():表示这个对象已经不再改变,或者它已经将它最近的改变通知给它的所有观察者,这样已经改变( )方法现在将返回错误的.
    Syntax : protected void clearChanged( )
    Exception : NA
    
    
    // Java code to demonstrate clearChanged() method 
    import java.util.*; 
      
    // This is the observer 
    class Observer1 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Inside Observer1"); 
        } 
    } 
      
    // This is the class being observed 
    class BeingObserved extends Observable 
    { 
        void func1() 
        { 
            setChanged(); 
            // clearChanged method removes all the changes made by setChanged method 
            clearChanged(); 
            notifyObservers(); 
              
        } 
    } 
      
    class ObserverDemo { 
    // Driver method of the program 
        public static void main(String args[])  
        { 
            BeingObserved beingObserved = new BeingObserved(); 
            Observer1 observer1 = new Observer1(); 
            beingObserved.addObserver(observer1); 
            beingObserved.func1(); 
        } 
    } 

    输出:

    No Output

    由于clearChanged()方法已删除所有更改,因此未获得任何输出。

  4. notifyObservers():通过调用通知调用对象的所有观察者它已更改更新( ).
    null 作为第二个参数传递给更新( ).
    Syntax : public void notifyObservers( )
    Exception : NA
    
    
    // Java code to demonstrate notifyObservers( ) method 
    import java.util.*; 
       
    // This is first observer 
    class Observer1 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer1 Notified"); 
        } 
    } 
      
    // This is second observer 
    class Observer2 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer2 Notified"); 
        } 
    } 
      
    // This is class being observed 
    class BeingObserved extends Observable 
    { 
        void func1() 
        { 
            setChanged(); 
            /*This method notifies the change to all the  
            observers that are registered*/
            notifyObservers(); 
              
        } 
    } 
      
    class ObserverDemo { 
        // Driver method of the program 
        public static void main(String args[])  
        { 
            BeingObserved beingObserved = new BeingObserved(); 
            Observer1 observer1 = new Observer1(); 
            Observer2 observer2 = new Observer2(); 
            beingObserved.addObserver(observer1); 
            beingObserved.addObserver(observer2); 
            beingObserved.func1(); 
        } 
    } 

    输出:

    Observer2 Notified
    Observer1 Notified
  5. 通知观察者(对象 obj):通过调用通知调用对象的所有观察者它已更改更新( ).
    对象作为参数传递给更新( ).
    Syntax : public void notifyObservers(Object obj)
    Exception : NA
    
    
    // Java code to demonstrate notifyObservers(Object obj) method 
    import java.util.*; 
      
    // This is first observer 
    class Observer1 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer1 Notified with value : " +  
                    ((Integer)arg).intValue()); 
        } 
    } 
      
    // This is second observer 
    class Observer2 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer2 Notified with value : " +  
                    ((Integer)arg).intValue()); 
        } 
    } 
      
    // This is class being observed 
    class BeingObserved extends Observable 
    { 
        void func1() 
        { 
            setChanged(); 
            /*This method notifies the change to all the  
            observers that are registered and passes an object*/
            notifyObservers(new Integer(10)); 
              
        } 
    } 
      
    class ObserverDemo { 
        // Driver method of the program 
        public static void main(String args[])  
        { 
            BeingObserved beingObserved = new BeingObserved(); 
            Observer1 observer1 = new Observer1(); 
            Observer2 observer2 = new Observer2(); 
            beingObserved.addObserver(observer1); 
            beingObserved.addObserver(observer2); 
            beingObserved.func1(); 
        } 
    } 

    输出:

    Observer2 Notified with value : 10
    Observer1 Notified with value : 10
  6. 计数观察者():返回观察调用对象的对象数量。
    Syntax : public int countObservers( )
    Returns : the number of observers of this object
    Exception : NA
    
    
    // Java code to demonstrate countObservers() method 
    import java.util.*; 
      
    // This is first observer 
    class Observer1 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer1"); 
        } 
    } 
      
    // This is second observer 
    class Observer2 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer2"); 
        } 
    } 
      
    // This is class being observed 
    class BeingObserved extends Observable 
    { 
        void func1() 
        { 
            setChanged(); 
            notifyObservers(); 
        } 
    } 
      
    class ObserverDemo { 
        // Driver method of the program 
        public static void main(String args[])  
        { 
            BeingObserved beingObserved = new BeingObserved(); 
            Observer1 observer1 = new Observer1(); 
            Observer2 observer2 = new Observer2(); 
            beingObserved.addObserver(observer1); 
            beingObserved.addObserver(observer2); 
            int count_observer = beingObserved.countObservers(); 
            System.out.println("Number of observers is " + count_observer); 
            beingObserved.func1(); 
        } 
    } 

    输出:

    Number of observers is 2
    Observer2
    Observer1
  7. 删除观察者(观察者观察者):删除观察者来自观察调用对象的对象列表。
    通过空值这个方法不会有任何效果。
    Syntax : public void deleteObserver(Observer observer)
    Exception : NA
    
    
    // Java code to demonstrate deleteObserver(Observer observer) method 
    import java.util.*; 
      
    // This is first observer 
    class Observer1 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer1"); 
        } 
    } 
      
    // This is second observer 
    class Observer2 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer2"); 
        } 
    } 
      
    // This is class being observed 
    class BeingObserved extends Observable 
    { 
        void func1() 
        { 
            setChanged(); 
            notifyObservers(); 
        } 
    } 
      
    class ObserverDemo { 
        // Driver method of the program 
        public static void main(String args[])  
        { 
            int count_observer; 
            BeingObserved beingObserved = new BeingObserved(); 
            Observer1 observer1 = new Observer1(); 
            Observer2 observer2 = new Observer2(); 
            beingObserved.addObserver(observer1); 
            beingObserved.addObserver(observer2); 
              
            count_observer = beingObserved.countObservers(); 
            System.out.println("Number of observers before" +  
            " calling deleteObserver(): " + count_observer); 
            beingObserved.func1(); 
              
            // Deleting observer1 
            beingObserved.deleteObserver(observer1); 
            count_observer = beingObserved.countObservers(); 
            System.out.println("No. of observers after"+  
            " calling deleteObserver(): " + count_observer); 
            beingObserved.func1(); 
              
        } 
    } 

    输出:

    Number of observers before calling deleteObserver(): 2
    Observer2
    Observer1
    No. of observers aftercalling deleteObserver(): 1
    Observer2
  8. deleteObservers():删除调用对象的所有观察者。
    Syntax : public void deleteObservers()
    Exception : NA
    
    
    // Java code to demonstrate deleteObservers() method 
    import java.util.*; 
      
    // This is first observer 
    class Observer1 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer1"); 
        } 
    } 
      
    // This is second observer 
    class Observer2 implements Observer 
    { 
        public void update(Observable obj, Object arg)  
        { 
            System.out.println("Observer2"); 
        } 
    } 
      
    // This is class being observed 
    class BeingObserved extends Observable 
    { 
        void func1() 
        { 
            setChanged(); 
            notifyObservers(new Integer(10)); 
        } 
    } 
      
    class ObserverDemo { 
        // Driver method of the program 
        public static void main(String args[])  
        { 
            int count_observer; 
            BeingObserved beingObserved = new BeingObserved(); 
            Observer1 observer1 = new Observer1(); 
            Observer2 observer2 = new Observer2(); 
            beingObserved.addObserver(observer1); 
            beingObserved.addObserver(observer2); 
              
            count_observer = beingObserved.countObservers(); 
            System.out.println("Number of observers before" +  
            " calling deleteObserver(): " + count_observer); 
            beingObserved.func1(); 
              
            // Deleting all observers 
            beingObserved.deleteObservers(); 
            count_observer = beingObserved.countObservers(); 
            System.out.println("No. of observers after "+  
            "calling deleteObserver(): " + count_observer); 
            beingObserved.func1(); 
              
        } 
    } 

    输出:

    Number of observers before calling deleteObserver(): 2
    Observer2
    Observer1
    No. of observers after calling deleteObserver(): 0

参考: Observables in Java



相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.util.Observable class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。