java中的触发事件

java中的触发事件,java,javabeans,Java,Javabeans,为什么PropertyChangeSupport类中的方法firePropertyChange(String propertyName、Object oldValue、Object newValue)不检查新旧值是否可以同时为null?我不确定您的意思,但这里的实际代码肯定比我试图解释的要干净:) /** *向侦听器报告绑定的属性更新 *已注册以跟踪的更新 *所有属性或具有指定名称的属性。 * *如果新旧值相等且非空,则不会激发任何事件。 * *这仅仅是一个方便的包装,围绕着更一般的问题 *{@

为什么PropertyChangeSupport类中的方法firePropertyChange(String propertyName、Object oldValue、Object newValue)不检查新旧值是否可以同时为null?

我不确定您的意思,但这里的实际代码肯定比我试图解释的要干净:)

/**
*向侦听器报告绑定的属性更新
*已注册以跟踪的更新
*所有属性或具有指定名称的属性。
*
*如果新旧值相等且非空,则不会激发任何事件。
*
*这仅仅是一个方便的包装,围绕着更一般的问题
*{@link#firePropertyChange(PropertyChangeEvent)}方法。
*
*@param propertyName已更改属性的编程名称
*@param oldValue属性的旧值
*@param newValue属性的新值
*/
public void firePropertyChange(字符串propertyName、对象oldValue、对象newValue){
如果(oldValue==null | | newValue==null | |!oldValue.equals(newValue)){
firePropertyChange(新的PropertyChangeEvent(this.source、propertyName、oldValue、newValue));
}
}

我不知道你的意思是什么,但这里的实际代码肯定比我试图解释的更清晰:)

/**
*向侦听器报告绑定的属性更新
*已注册以跟踪的更新
*所有属性或具有指定名称的属性。
*
*如果新旧值相等且非空,则不会激发任何事件。
*
*这仅仅是一个方便的包装,围绕着更一般的问题
*{@link#firePropertyChange(PropertyChangeEvent)}方法。
*
*@param propertyName已更改属性的编程名称
*@param oldValue属性的旧值
*@param newValue属性的新值
*/
public void firePropertyChange(字符串propertyName、对象oldValue、对象newValue){
如果(oldValue==null | | newValue==null | |!oldValue.equals(newValue)){
firePropertyChange(新的PropertyChangeEvent(this.source、propertyName、oldValue、newValue));
}
}
可能会提供一些线索:

如果旧值和新值的 真实值是未知的

事件源可以发送空对象作为名称,以指示 如果其属性已更改,则为任意一组。在这种情况下,旧的 新值也应该为null

因此,当
source==null
时,
oldValue==null
newValue==null
看起来也有一些特殊的含义。因此,当两个值都为
空时,可能希望始终传播更改,即使它们是相同的。

可能会给出一些提示:

如果旧值和新值的 真实值是未知的

事件源可以发送空对象作为名称,以指示 如果其属性已更改,则为任意一组。在这种情况下,旧的 新值也应该为null

因此,当
source==null
时,
oldValue==null
newValue==null
看起来也有一些特殊的含义。因此,当两个值都是
null
时,您可能希望始终传播更改,即使它们相同。

您的意思是“为什么
firePropertyChange(字符串、对象、对象)不更改?”
处理两个对象都等于
null的情况
处理对象等于
Object#的情况的方法与处理对象等于
的情况的方法相同?您的意思是“为什么
firePropertyChange(字符串、对象、对象)不改变?”
处理两个对象都等于
null的情况
处理对象等于
Object#equals
“?
/**
     * Reports a bound property update to listeners
     * that have been registered to track updates of
     * all properties or a property with the specified name.
     * <p>
     * No event is fired if old and new values are equal and non-null.
     * <p>
     * This is merely a convenience wrapper around the more general
     * {@link #firePropertyChange(PropertyChangeEvent)} method.
     *
     * @param propertyName  the programmatic name of the property that was changed
     * @param oldValue      the old value of the property
     * @param newValue      the new value of the property
     */
    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
            firePropertyChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
        }
    }