Java Observable notifyObservers(Object arg) 方法



描述

Java Observable notifyObservers(Observer o) 方法会通知所有观察者对象是否发生了更改。此方法稍后会调用clearChanged 方法以指示此对象不再发生更改。此方法等效于notifyObservers(null)

声明

以下是Java.util.Observable.notifyObservers(Object arg) 方法的声明

public void notifyObservers(Object arg)

参数

org - 任何对象

返回值

异常

为字符串值更改通知观察者示例

以下示例演示了 java.util.Observable.notifyObservers() 方法的用法。我们通过扩展 Observable 类创建了一个 ObservedObject 类,然后覆盖了其 setValue() 方法并使用 notifyObservers() 方法,通知了观察者。在主类中,我们使用 addObserver() 方法添加了观察者,在输出中,我们打印了 ObservedObject 值发生的更新。

package com.tutorialspoint;

import java.util.Observable;
import java.util.Observer;

class ObservedObject extends Observable {
   private String watchedValue;
   
   public ObservedObject(String value) {
      watchedValue = value;
   }
   
   public void setValue(String value) {
      
      // if value has changed notify observers
      if(!watchedValue.equals(value)) {
         System.out.println("Value changed to new value: "+value);
         watchedValue = value;

         // mark as value changed
         setChanged();
         // trigger notification
         notifyObservers(value);
      }
   }
}
public class ObservableDemo implements Observer {
   public String name;
  
   public ObservableDemo(String name) {
      this.name = name;
   }
   
   public static void main(String[] args) {
      
      // create watched and watcher objects
      ObservedObject watched = new ObservedObject("Original Value");
      
      // watcher object listens to object change
      ObservableDemo watcher = new ObservableDemo("Watcher");

      // add observer to the watched object
      watched.addObserver(watcher);
      
      // trigger value change
      watched.setValue("New Value");
   }
      
   public void update(Observable obj, Object arg) {
      System.out.println(name+" called with Arguments: "+arg);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Value changed to new value: New Value
Watcher called with Arguments: New Value

为整数值更改通知观察者示例

以下示例演示了 java.util.Observable.notifyObservers(Object) 方法的用法。我们通过扩展 Observable 类创建了一个 ObservedObject 类,然后覆盖了其 setValue() 方法并使用 notifyObservers() 方法,通知了观察者。在主类中,我们使用 addObserver() 方法添加了观察者,在输出中,我们打印了 ObservedObject 值发生的更新。

package com.tutorialspoint;

import java.util.Observable;
import java.util.Observer;

class ObservedObject extends Observable {
   private Integer watchedValue;
   
   public ObservedObject(Integer value) {
      watchedValue = value;
   }
   
   public void setValue(Integer value) {
      
      // if value has changed notify observers
      if(!watchedValue.equals(value)) {
         System.out.println("Value changed to new value: "+value);
         watchedValue = value;

         // mark as value changed
         setChanged();
         // trigger notification
         notifyObservers(value);
      }
   }
}
public class ObservableDemo implements Observer {
   public String name;
  
   public ObservableDemo(String name) {
      this.name = name;
   }
   
   public static void main(String[] args) {
      
      // create watched and watcher objects
      ObservedObject watched = new ObservedObject(1);
      
      // watcher object listens to object change
      ObservableDemo watcher = new ObservableDemo("Watcher");

      // add observer to the watched object
      watched.addObserver(watcher);
      
      // trigger value change
      watched.setValue(2);
   }
      
   public void update(Observable obj, Object arg) {
      System.out.println(name+" called with Arguments: "+arg);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Value changed to new value: 2
Watcher called with Arguments: 2

为对象值更改通知观察者示例

以下示例演示了 java.util.Observable.notifyObservers(Object) 方法的用法。我们通过扩展 Observable 类创建了一个 ObservedObject 类,然后覆盖了其 setValue() 方法并使用 notifyObservers() 方法,通知了观察者。在主类中,我们使用 addObserver() 方法添加了观察者,在输出中,我们打印了 ObservedObject 值发生的更新。

package com.tutorialspoint;

import java.util.Observable;
import java.util.Observer;

class ObservedObject extends Observable {
   private Student watchedValue;
   
   public ObservedObject(Student value) {
      watchedValue = value;
   }
   
   public void setValue(Student value) {
      
      // if value has changed notify observers
      if(!watchedValue.equals(value)) {
         System.out.println("Value changed to new value: "+value);
         watchedValue = value;

         // mark as value changed
         setChanged();
         // trigger notification
         notifyObservers(value);
      }
   }
}
public class ObservableDemo implements Observer {
   public String name;
  
   public ObservableDemo(String name) {
      this.name = name;
   }
   
   public static void main(String[] args) {
      
      // create watched and watcher objects
      ObservedObject watched = new ObservedObject(new Student(1, "Julie"));
      
      // watcher object listens to object change
      ObservableDemo watcher = new ObservableDemo("Watcher");

      // add observer to the watched object
      watched.addObserver(watcher);
      
      // trigger value change
      watched.setValue(new Student(2, "Robert"));
   }
      
   public void update(Observable obj, Object arg) {
      System.out.println(name+" called with Arguments: "+arg);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Value changed to new value: [ 2, Robert ]
Watcher called with Arguments: [ 2, Robert ]
java_util_observable.htm
广告

© . All rights reserved.