- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包额外内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
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 ]