- 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() 方法
描述
Java Observable notifyObservers() 方法通知所有观察者对象已更改。此方法之后调用clearChanged方法,以指示此对象不再更改。此方法等效于notifyObservers(null)。
声明
以下是Java.util.Observable.notifyObservers() 方法的声明
public void notifyObservers()
参数
无
返回值
无
异常
无
针对字符串值更改示例的通知观察者
以下示例演示了 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();
}
}
}
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("Update called");
}
}
输出
让我们编译并运行上述程序,这将产生以下结果:
Value changed to new value: New Value Update called
针对整数值更改示例的通知观察者
以下示例演示了 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 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();
}
}
}
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("Update called");
}
}
输出
让我们编译并运行上述程序,这将产生以下结果:
Value changed to new value: 2 Update called
针对对象值更改示例的通知观察者
以下示例演示了 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 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();
}
}
}
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("Update called");
}
}
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 ] Update called