Observable ialah kelas dalam bahasa pengaturcaraan Java yang membolehkan anda membina subkelas yang boleh diperhatikan oleh bahagian lain program. Kelas pemerhatian dimaklumkan apabila objek subkelas ini berubah. Apabila pemerhati dimaklumkan tentang perubahan, kaedah kemas kini() dipanggil. Kelas Observable tersedia dalam pakej java.util. Subkelas kelas boleh digunakan untuk menerangkan objek yang perlu diperhatikan oleh aplikasi, dan juga, mungkin terdapat satu atau lebih pemerhati pada objek yang boleh diperhatikan. Kelas pemerhati harus melaksanakan antara muka Pemerhati, yang menentukan kaedah kemas kini(), yang mesti dilaksanakan oleh kelas pemerhatian.
IKLAN Kursus Popular dalam kategori ini JAVA MASTERY - Pengkhususan | 78 Siri Kursus | 15 Ujian Olok-olokSesuatu objek dalam pemerhatian mesti mematuhi dua peraturan asas:
Sebelum kemas kini(), objek yang diperhatikan mesti memanggil kedua-dua kaedah setChanged() dan notifyObservers().
Pengisytiharan kelas Boleh Diperhatikan.
Pengisytiharan untuk kelas java.util.Observable adalah seperti berikut:
public class Observable extends Object
Pembina Kelas Boleh Diperhatikan
Diberikan di bawah ialah pembina kelas boleh diperhatikan:
Diberikan di bawah adalah kaedah kelas boleh diperhatikan:
Dalam program, interaksi antara pemerhati dan pemerhati biasanya berbentuk urutan peristiwa berikut.
Diberikan di bawah adalah contoh Observable dalam Java:
Contoh untuk Observable dalam Java untuk melakukan perubahan dengan atau tanpa kaedah setChanged().
Kod:
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); } }
Output:
Seperti dalam atur cara di atas, ObservableEx kelas takrif pengguna yang boleh diperhatikan dicipta dengan melanjutkan kelas boleh diperhatikan, dan juga kelas takrifan pengguna Observer ObserverEx dicipta dengan melaksanakan antara muka Observer di mana kelas menyediakan pelaksanaan untuk kemas kini( ) kaedah. Seterusnya, kelas ObservableEx mengandungi dua kaedah change_with_setChanged() dan 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.
Atas ialah kandungan terperinci Boleh diperhatikan di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!