javaSE观察者模式Observer和Observable--相关api的翻译

来源:互联网 发布:淘宝手机端流量怎么看 编辑:程序博客网 时间:2024/05/05 15:46
相关api的翻译,翻译可能存在误差,有问题谢谢留言。译文都是在原文下方可自行参考。

Java™ Platform, Standard Edition 8
API Specification


java.util Interface Observer

  • public interface Observer
    A class can implement the Observer interface when it wants to be informed of changes in observable objects.
    任何类都可以实现Observer接口,实现该接口的类,可以获取相关被观察者(observable)对象的更改情况。
    Since:
    JDK1.0
    See Also:
    Observable
    • Method Summary

      All MethodsInstance MethodsAbstract MethodsModifier and TypeMethod and Descriptionvoidupdate(Observable o, Object arg)
      This method is called whenever the observed object is changed.
      当被观察者(observed)对象发生变动时,该方法将被调用。
    • 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's notifyObservers 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 the notifyObservers method. 被观察者notifyObservers方法传递过来的一个Object参数。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

java.util   Class Observable

  • java.lang.Object
    • java.util.Observable

  • public class Observableextends 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's notifyObservers method causes all of its observers to be notified of the change by a call to their update 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

      ConstructorsConstructor and DescriptionObservable()
      Construct an Observable with zero Observers.构建有零个观察者的被观察者对象。
    • Method Summary

      All MethodsInstance MethodsConcrete MethodsModifier and TypeMethod and DescriptionvoidaddObserver(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 voidclearChanged()
      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.
      标识被观察者对象不再改变,
      或者已近通知了所有观察者最近的改变,
      那么将导致hasChanged方法返回false。
      intcountObservers()
      Returns the number of observers of this Observable object.
      返回观察者集中观察者的总个数。
      voiddeleteObserver(Observer o)
      Deletes an observer from the set of observers of this object.
      从观察者集中删除一个指定的观察者。
      voiddeleteObservers()
      Clears the observer list 
      so that this object no longer has any observers.
      清空观察者集。
      booleanhasChanged()
      Tests if this object has changed.
      测试被观察者对象是否有改变
      (相当于获取当前被观察者是否发生改变的状态)。
      voidnotifyObservers()
      If this object has changed, as indicated by the hasChanged method, 
      then notify all of its observers 
      and then call the clearChanged method to indicate 
      that this object has no longer changed.
      如果被观察者发生改变,就如通过调用hasChanged方法一样
      ,然后通知所有的观察者
      ,然后调用clearChanged方法让该对象不再改变。
      voidnotifyObservers(Object arg)
      If this object has changed, as indicated by the hasChanged method, 
      then notify all of its observers and then call the clearChanged method 
      to indicate that this object has no longer changed.
      如果被观察者发生改变,就如通过调用hasChanged方法一样
      ,然后通知所有的观察者
      ,然后调用clearChanged方法让该对象不再改变。
      protected voidsetChanged()
      Marks this Observable object as having been changed;
       the hasChanged method will now returntrue.
      标记被观察者对象已经发生改变,hasChanged方法将返回true。
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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. Passing null 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 the hasChanged method, then notify all of its observers and then call the clearChangedmethod 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 null. 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 the hasChanged method, then notify all of its observers and then call the clearChangedmethod 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 the arg 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 the notifyObservers methods.
        See Also:
        notifyObservers()notifyObservers(java.lang.Object)
      • hasChanged

        public boolean hasChanged()
        Tests if this object has changed.
        测试被观察者对象是否有改变
        (相当于获取当前被观察者是否发生改变的状态)
        Returns:
        true if and only if the setChanged method has been called more recently than the clearChanged 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.








如果被观察者发生改变,就如通过调用hasChanged方法一样
,然后通知所有的观察者
,然后调用clearChanged方法让该对象不再改变。
0 0
原创粉丝点击