observable_Java Observable notifyObservers()方法与示例
observable
可觀察的類notifyObservers()方法 (Observable Class notifyObservers() method)
Syntax:
句法:
public void notifyObservers();public void notifyObservers(Object o);notifyObservers() method is available in java.util package.
notifyObservers()方法在java.util包中可用。
notifyObservers() method is used to notify all of its observers in the list when the object has changed.
當對象已更改時, notifyObservers()方法用于通知列表中的所有觀察者。
notifyObservers(Object o) method is used to notify all of its observers in the list when this object has changed and later will invoke the clearChanged() to denotes that this object has no longer required to change.
notifyObservers(Object o)方法用于在此對象發生更改時通知其列表中的所有觀察者,稍后將調用clearChanged()表示該對象不再需要更改。
These methods don't throw an exception at the time of notifying an observer.
在通知觀察者時,這些方法不會引發異常。
These are non-static methods and it is accessible with the class object and if we try to access these methods with the class name then also we will get an error.
這些是非靜態方法,可通過類對象訪問,如果嘗試使用類名訪問這些方法,則也會收到錯誤消息。
Parameter(s):
參數:
In the first case, notifyObservers() – it does not accept any parameter.
在第一種情況下, notifyObservers() –它不接受任何參數。
In the first case, notifyObservers(Object o) – Object o – represents an object of any type.
在第一種情況下, notifyObservers(Object o) – Object o –表示任何類型的對象。
Return value:
返回值:
In both the cases, the return type of the method is void, it returns nothing.
在這兩種情況下,方法的返回類型都是void ,它什么也不返回。
Example 1:
范例1:
// Java program to demonstrate the example // of notifyObservers() method of Observableimport java.util.*;// Implement Observers class class Observers_1 implements Observer {public void update(Observable obj, Object ob) {System.out.println("Obs1 is notified");} }// Implement Observed Class class Observed extends Observable {// Function call with setChanged()void notifyObserver() {setChanged();// By using notifyObservers() method is // to notify all the observers that are// implementednotifyObservers();} }public class NotifyObserver {// Implement Main Methodpublic static void main(String args[]) {Observed observed = new Observed();Observers_1 obs1 = new Observers_1();observed.addObserver(obs1);observed.notifyObserver();} }Output
輸出量
Obs1 is notifiedExample 2:
范例2:
import java.util.*;class Observers_2 implements Observer {public void update(Observable obj, Object ob) {System.out.println("Obs2 is notified: " + ((Float) ob).floatValue());} }// Implement Observed Class class Observed extends Observable {// Function call with setChanged()void notifyObserver1() {setChanged();// By using notifyObservers() method is // to notify all the observers that are// implementednotifyObservers();}void notifyObserver2() {setChanged();// By using notifyObservers() method is // to notify all the observers that are// implemented with the given objectnotifyObservers(new Float(10.0));}}public class NotifyObserver {// Implement Main Methodpublic static void main(String args[]) {Observed observed = new Observed();Observers_2 obs2 = new Observers_2();observed.addObserver(obs2);observed.notifyObserver2();} }Output
輸出量
Obs2 is notified: 10.0翻譯自: https://www.includehelp.com/java/observable-notifyobservers-method-with-example.aspx
observable
總結
以上是生活随笔為你收集整理的observable_Java Observable notifyObservers()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot + Shardin
- 下一篇: java 方法 示例_Java扫描仪的h