- 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 clearChanged() 方法
描述
Java Observable clearChanged(Observer o) 方法表示該物件不再發生變化,或者它已經通知了所有觀察者其最近的變化。此方法由notifyObservers方法自動呼叫。
宣告
以下是Java.util.Observable.clearChanged()方法的宣告
protected void clearChanged()
引數
無
返回值
無
異常
無
為字串值更改的觀察者清除更改示例
以下示例演示了 java.util.Observable.clearChanged() 方法的使用。我們透過擴充套件 Observable 類並覆蓋其 resetValue() 方法(呼叫 clearChanged() 方法)建立了一個 ObservedObject 類。在主類中,我們使用 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();
}
}
public void resetValue() {
// reset value changed flag
clearChanged();
}
}
public class ObservableDemo implements Observer {
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
System.out.println("setValue method called...");
watched.setValue("New Value");
// check if value has changed
if(watched.hasChanged()) {
System.out.println("Value changed");
} else {
System.out.println("Value not changed");
}
// trigger reset
System.out.println("resetValue method called...");
watched.resetValue();
// check if value has changed
if(watched.hasChanged()) {
System.out.println("Value changed");
} else {
System.out.println("Value not changed");
}
}
public void update(Observable obj, Object arg) {
System.out.println("Update called with Arguments: "+arg);
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
setValue method called... Value changed resetValue method called... Value not changed
為整數值更改的觀察者清除更改示例
以下示例演示了 java.util.Observable.clearChanged() 方法的使用。我們透過擴充套件 Observable 類並覆蓋其 resetValue() 方法(呼叫 clearChanged() 方法)建立了一個 ObservedObject 類。在主類中,我們使用 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();
}
}
public void resetValue() {
// reset value changed flag
clearChanged();
}
}
public class ObservableDemo implements Observer {
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
System.out.println("setValue method called...");
watched.setValue(2);
// check if value has changed
if(watched.hasChanged()) {
System.out.println("Value changed");
} else {
System.out.println("Value not changed");
}
// trigger reset
System.out.println("resetValue method called...");
watched.resetValue();
// check if value has changed
if(watched.hasChanged()) {
System.out.println("Value changed");
} else {
System.out.println("Value not changed");
}
}
public void update(Observable obj, Object arg) {
System.out.println("Update called with Arguments: "+arg);
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
setValue method called... Value changed resetValue method called... Value not changed
為物件值更改的觀察者清除更改示例
以下示例演示了 java.util.Observable.clearChanged() 方法的使用。我們透過擴充套件 Observable 類並覆蓋其 resetValue() 方法(呼叫 clearChanged() 方法)建立了一個 ObservedObject 類。在主類中,我們使用 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();
}
}
public void resetValue() {
// reset value changed flag
clearChanged();
}
}
public class ObservableDemo implements Observer {
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
System.out.println("setValue method called...");
watched.setValue(new Student(2, "Robert"));
// check if value has changed
if(watched.hasChanged()) {
System.out.println("Value changed");
} else {
System.out.println("Value not changed");
}
// trigger reset
System.out.println("resetValue method called...");
watched.resetValue();
// check if value has changed
if(watched.hasChanged()) {
System.out.println("Value changed");
} else {
System.out.println("Value not changed");
}
}
public void update(Observable obj, Object arg) {
System.out.println("Update 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 + " ]";
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
setValue method called... Value changed resetValue method called... Value not changed