java.util.Observable用於創建程序其他部分可以觀察到的子類。當此類子類的對象發生更改時,觀察類會收到通知。這更新( )當觀察者收到更改通知時調用該方法。
Note: Observing class must implement the Observer interface, which defines the update( ) method.
被觀察的對象必須遵循兩個簡單的規則:
- 如果改變,必須調用設置更改()方法。
- 當它準備好通知觀察者此更改時,它必須調用通知觀察者()方法。這導致更新( )要調用的觀察對象中的方法。
Be careful, if the object calls notifyObservers( ) method without having previously called setChanged( ) method, no action will take place.
被觀察對象必須同時調用設置更改()和通知觀察者()方法,之前更新( )將被調用。
java.util.Observable 的構造函數:
- 可觀察()
構造一個具有零個觀察者的 Observable。
方法:
- 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
- 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
- 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()方法已刪除所有更改,因此未獲得任何輸出。
- 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
- 通知觀察者(對象 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
- 計數觀察者():返回觀察調用對象的對象數量。
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
- 刪除觀察者(觀察者觀察者):刪除觀察者來自觀察調用對象的對象列表。
通過空值這個方法不會有任何效果。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
- 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
相關用法
- Java Java.util.Observable.addObserver()用法及代碼示例
- Java Java.util.Observable.clearChanged()用法及代碼示例
- Java Java.util.Observable.countObservers()用法及代碼示例
- Java Java.util.Observable.deleteObserver()用法及代碼示例
- Java Java.util.Observable.deleteObservers()用法及代碼示例
- Java Java.util.Observable.hasChanged()用法及代碼示例
- Java Java.util.Observable.notifyObservers()用法及代碼示例
- Java Java.util.Observable.setChanged()用法及代碼示例
- Java Java.util.Objects用法及代碼示例
- Java Java.util.ArrayDeque.add()用法及代碼示例
- Java Java.util.ArrayDeque.addFirst()用法及代碼示例
- Java Java.util.ArrayDeque.addLast()用法及代碼示例
- Java Java.util.ArrayDeque.clear()用法及代碼示例
- Java Java.util.ArrayDeque.clone()用法及代碼示例
- Java Java.util.ArrayDeque.descendingIterator()用法及代碼示例
- Java Java.util.ArrayDeque.element()用法及代碼示例
- Java Java.util.ArrayDeque.getFirst()用法及代碼示例
- Java Java.util.ArrayDeque.getLast()用法及代碼示例
- Java Java.util.ArrayDeque.isEmpty()用法及代碼示例
- Java Java.util.ArrayDeque.iterator()用法及代碼示例
- Java Java.util.ArrayDeque.peek()用法及代碼示例
- Java Java.util.ArrayDeque.peekFirst()用法及代碼示例
- Java Java.util.ArrayDeque.peekLast()用法及代碼示例
- Java Java.util.ArrayDeque.poll()用法及代碼示例
- Java Java.util.ArrayDeque.pollFirst()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.util.Observable class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。