Observable 是 Java 编程语言中的一个类,它允许您构造程序其他部分可以观察到的子类。当该子类的对象发生更改时,观察类会收到通知。当观察者收到更改通知时,将调用 update() 方法。 Observable 类在 java.util 包中可用。该类的子类可以用来描述应用程序需要观察的对象,并且一个可观察对象上可能有一个或多个观察者。观察类应该实现 Observer 接口,该接口指定了观察类必须实现的 update() 方法。
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试被观察的对象必须遵守两个基本规则:
在update()之前,被观察对象必须同时调用setChanged()和notifyObservers()方法。
Observable 类的声明。
java.util.Observable 类的声明如下:
public class Observable extends Object
Observable 类的构造函数
下面给出的是可观察类的构造函数:
下面给出的是可观察类的方法:
在程序中,可观察者和观察者之间的交互通常采用以下事件序列的形式。
下面给出了 Java 中 Observable 的示例:
Java 中 Observable 使用或不使用 setChanged() 方法执行更改的示例。
代码:
import java.util.*; // This is the observer class class ObserverEx implements Observer { public void update(Observable obj, Object arg) { System.out.println("Update in an observer side."); } } // This is the obsrvable class class ObservableEx extends Observable { void change_with_setChanged() { setChanged(); System.out.println("Change the status with setChanged : " + hasChanged()); notifyObservers(); } void change_without_setChanged() { System.out.println("Change status with setChanged : " + hasChanged()); notifyObservers(); } } public class HelloWorld { public static void main(String args[]) { ObservableEx Observable = new ObservableEx(); ObserverEx observer1 = new ObserverEx(); ObserverEx observer2 = new ObserverEx(); Observable.addObserver(observer1); Observable.addObserver(observer2); Observable.change_with_setChanged(); Observable.change_without_setChanged(); int no = Observable.countObservers(); System.out.println("The number of observers for this Observable are : " + no); } }
输出:
如上面的程序,Observable用户定义类ObservableEx是通过扩展Observable类创建的,Observer用户定义类ObserverEx是通过实现Observer接口创建的,该类提供了更新的实现( ) 方法。接下来,ObservableEx类包含两个方法change_with_setChanged()和change_without_setChanged()。
The method change_with_setChanged() call the setChanged() and then notify all the observer, which means the changes done here with setChanged will be notified to all the observer.Whereas the method change_without_setChanged() does not call the setChanged() and notify all the observers, which means the changes done here without setChanged will not show to all the observer.
Then, in the main function, one Observable and two Observer objects are created, and also add both the Observer object to this Observable. Next, on Observer objects, the change_with_setChanged() method is called, which notifies both the observers and called the update() method, which prints the message, whereas the change_without_setChanged() method does not call the update() method of the observers. And next finding and printing the number of observers, as we can see in the above output.
Example for Observable in Java to perform changes with or without clearChanged() method.
Code:
import java.util.*; // This is the observer class class ObserverEx implements Observer { public void update(Observable obj, Object arg) { System.out.println("Update in an observer side."); } } // This is the obsrvable class class ObservableEx extends Observable { void change_with_clearChanged() { setChanged(); System.out.println("Removes all the changes made by setChanged method."); // clearChanged method clearChanged(); notifyObservers(); } void change_without_clearChanged() { setChanged(); System.out.println("Does not removes all the changes made by setChanged method. "); notifyObservers(); } } public class HelloWorld { public static void main(String args[]) { ObservableEx Observable = new ObservableEx(); ObserverEx observer1 = new ObserverEx(); ObserverEx observer2 = new ObserverEx(); Observable.addObserver(observer1); Observable.addObserver(observer2); Observable.change_with_clearChanged(); Observable.change_without_clearChanged(); int no = Observable.countObservers(); System.out.println("The number of observers for this Observable are : " + no); Observable.deleteObserver(observer2); no = Observable.countObservers(); System.out.println("The number of observers after delete for this Observable are : " + no); } }
Output:
As in the above program, the classes ObservableEx and ObserverEx are created. Next, the class ObservableEx contains two methods change_with_clearChanged() and change_without_clearChanged(). The method change_with_clearChanged() call the setChanged(), clearChanged() which removes all the changes made by setChanged method. Whereas the method change_without_clearChanged() does not call the clearChanged() which means the changes made by setChanged method will not remove.
Then, in the main function, one Observable and two Observer objects are created, and also add both the Observer object to this Observable. Next, on Observer objects, the change_with_clearChanged() method is called, which does not call the update() method of the observers, whereas the change_without_setChanged() method calls the update() method of the observers. And next, delete the observer1 and finding reaming Observer and printing, as we can see in the above output.
The Observable class is available in java.util package. An Observable is a class in Java that allows the creation of an Observable subclass that other sections of the program can observe.
以上是Java 中的可观察性的详细内容。更多信息请关注PHP中文网其他相关文章!