Java™ Platform, Standard Edition 8
API Specification
java.util Interface Observer
-
public interface Observer
A class can implement theObserver
interface when it wants to be informed of changes in observable objects.任何类都可以实现Observer接口,实现该接口的类,可以获取相关被观察者(observable)对象的更改情况。
Since:
JDK1.0
See Also:
Observable
-
-
Method Detail
-
update
void update(Observable o, Object arg)
This method is called whenever the observed object is changed. An application calls an Observable object'snotifyObservers
method to have all the object's observers notified of the change.当被观察者(observed)对象发生变动时,该方法将被调用。当程序调用被观察者(Observale)对象的notifyObservers方法时,这个被观察者的所有观察者都会收到对象改变的通知。
Parameters:
o
- the observable object. 接受被观察的对象arg
- an argument passed to thenotifyObservers
method. 被观察者notifyObservers方法传递过来的一个Object参数。
-
-
java.util Class Observable
- java.lang.Object
-
- java.util.Observable
-
public class Observable extends Object
This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed.Observalbe代表一个被观察的对象,或者叫模型视图样式中的“数据”。它能被子类化,其子类代表程序想要观察的对象。
Since:
JDK1.0
See Also:
notifyObservers()
,notifyObservers(java.lang.Object)
,Observer
,Observer.update(java.util.Observable, java.lang.Object)
-
An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the
Observable
'snotifyObservers
method causes all of its observers to be notified of the change by a call to theirupdate
method.
一个被观察者对象可以拥有一个或多个观察者。观察者可以是任意实现了Observer接口的对象。当一个被观察实例发生改变时,程序会调用Observalbe(被观察对象)的notifyObservers方法,使观察者收到被观察者改变的通知让观察者调用update方法。The order in which notifications will be delivered is unspecified. The default implementation provided in the Observable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose.
消息发送的次序是未被指明的。默认情况下Observable的子类将使用与观察者注册的相关的顺序。但是子类有可能改变这一顺序,如使用没有保证的顺序,在分离的线程中发送通知;或者保证子类自己允许的顺序。Note that this notification mechanism has nothing to do with threads and is completely separate from the wait and notify mechanism of class Object.
通知的机制与线程是没有关系的,而且与Object的wait和notify机制也是分离的。When an observable object is newly created, its set of observers is empty. Two observers are considered the same if and only if the equalsmethod returns true for them.
当创建一个新的观察者对象时,它的观察者集是为空的。只有在equals方法返回为true时才认为两个观察者是相同的(指的是被观察者在添加观察者时对观察者的判定)。
-
-
Constructor Summary
Constructors Constructor and Description Observable()
Construct an Observable with zero Observers.构建有零个观察者的被观察者对象。
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description void
addObserver(Observer o)
Adds an observer to the set of observers for this object,provided that it is not the same as some observer already in the set.增加一个观察者到被观察者的观察者集中,添加的观察者必须是观察者集中不存在的观察者。protected void
clearChanged()
Indicates that this object has no longer changed,or that it has already notified all of its observersof its most recent change,so that the hasChanged method will now return false.标识被观察者对象不再改变,或者已近通知了所有观察者最近的改变,那么将导致hasChanged方法返回false。int
countObservers()
Returns the number of observers of this Observable object.返回观察者集中观察者的总个数。void
deleteObserver(Observer o)
Deletes an observer from the set of observers of this object.从观察者集中删除一个指定的观察者。void
deleteObservers()
Clears the observer listso that this object no longer has any observers.清空观察者集。boolean
hasChanged()
Tests if this object has changed.测试被观察者对象是否有改变(相当于获取当前被观察者是否发生改变的状态)。void
notifyObservers()
If this object has changed, as indicated by thehasChanged
method,then notify all of its observersand then call theclearChanged
method to indicatethat this object has no longer changed.如果被观察者发生改变,就如通过调用hasChanged方法一样,然后通知所有的观察者,然后调用clearChanged方法让该对象不再改变。void
notifyObservers(Object arg)
If this object has changed, as indicated by thehasChanged
method,then notify all of its observers and then call theclearChanged
methodto indicate that this object has no longer changed.如果被观察者发生改变,就如通过调用hasChanged方法一样,然后通知所有的观察者,然后调用clearChanged方法让该对象不再改变。protected void
setChanged()
Marks this Observable object as having been changed;the hasChanged method will now returntrue.标记被观察者对象已经发生改变,hasChanged方法将返回true。
-
-
-
Constructor Detail
-
Observable
public Observable()
Construct an Observable with zero Observers. 构建有零个观察者的被观察者对象。
-
-
Method Detail
-
addObserver
public void addObserver(Observer o)
Adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set. The order in which notifications will be delivered to multiple observers is not specified. See the class comment.增加一个观察者到被观察者的观察者集中,添加的观察者必须是观察者集中不存在的观察者。这个通知多个观察者的顺序是没有被指定的。参见之前的类注释。Parameters:
o
- an observer to be added. 一个将被添加观察者对象。Throws:
NullPointerException
- if the parameter o is null.如果传递过来的观察者对象为空,则会产生该异常。
-
deleteObserver
public void deleteObserver(Observer o)
Deletes an observer from the set of observers of this object. Passingnull
to this method will have no effect. 从被观察者的观察者集中删除o对象。如果o对象为null那么该方法将不会产生任何效果。Parameters:
o
- the observer to be deleted.需要删除的Observer对象。
-
notifyObservers
public void notifyObservers()
If this object has changed, as indicated by thehasChanged
method, then notify all of its observers and then call theclearChanged
method to indicate that this object has no longer changed.如果被观察者发生改变,就如通过调用hasChanged方法一样,然后通知所有的观察者,然后调用clearChanged方法让该对象不再改变。Each observer has its
update
method called with two arguments: this observable object andnull
. In other words, this method is equivalent to:notifyObservers(null)
每个观察这都将调用有两个参数的update方法,其中两个参数一个是Observable对象一个为null。换句话说该方法等同于notifyObservers(null).See Also:
clearChanged()
,hasChanged()
,Observer.update(java.util.Observable, java.lang.Object)
-
notifyObservers
public void notifyObservers(Object arg)
If this object has changed, as indicated by thehasChanged
method, then notify all of its observers and then call theclearChanged
method to indicate that this object has no longer changed.如果被观察者发生改变,就如通过调用hasChanged方法一样,然后通知所有的观察者,然后调用clearChanged方法让该对象不再改变。Each observer has its
update
method called with two arguments: this observable object and thearg
argument.
每个观察这都将调用有两个参数的update方法,其中两个参数一个是Observable对象一个为arg。Parameters:
arg
- any object.一个Object对象See Also:
clearChanged()
,hasChanged()
,Observer.update(java.util.Observable, java.lang.Object)
-
deleteObservers
public void deleteObservers()
Clears the observer list so that this object no longer has any observers.
清空观察者集,不再拥有任何观察者。
-
setChanged
protected void setChanged()
Marks this Observable object as having been changed; the hasChanged method will now return true.
-
clearChanged
protected void clearChanged()
Indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent change, so that the hasChanged method will now return false. This method is called automatically by thenotifyObservers
methods.See Also:
notifyObservers()
,notifyObservers(java.lang.Object)
-
hasChanged
public boolean hasChanged()
Tests if this object has changed.测试被观察者对象是否有改变(相当于获取当前被观察者是否发生改变的状态)Returns:
true
if and only if thesetChanged
method has been called more recently than theclearChanged
method on this object;false
otherwise. 当且仅当对象调用setChanged方法是返回true,而不是在最近调用了clearChanged方法。其他情况返回false。See Also:
clearChanged()
,setChanged()
-
countObservers
public int countObservers()
Returns the number of observers of this Observable object. 返回被观察者的观察者集中 观察者的总数。Returns:
the number of observers of this object.
-
-