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 Observable
import 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
// implemented
notifyObservers();
}
}
public class NotifyObserver {
// Implement Main Method
public static void main(String args[]) {
Observed observed = new Observed();
Observers_1 obs1 = new Observers_1();
observed.addObserver(obs1);
observed.notifyObserver();
}
}
Output
输出量
Obs1 is notified
Example 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
// implemented
notifyObservers();
}
void notifyObserver2() {
setChanged();
// By using notifyObservers() method is
// to notify all the observers that are
// implemented with the given object
notifyObservers(new Float(10.0));
}
}
public class NotifyObserver {
// Implement Main Method
public 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