java SimpleStringProperty set() 与 setValue()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16234669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:19:35  来源:igfitidea点击:

SimpleStringProperty set() vs. setValue()

javajavafx-2javabeans

提问by stefan.at.wpf

What is the difference between set(String)and setValue(String)in the SimpleStringPropertyclass?

之间有什么区别set(String),并setValue(String)SimpleStringProperty上课吗?

I know that set(String)is derived from StringPropertyBase, but this makes me even more wonder, why there additionally is setValue(String)?

我知道它set(String)是从 派生的StringPropertyBase,但这让我更加疑惑,为什么还有setValue(String)

回答by Sergey Grinev

set/setValueand get/getValuemethods pairs exist to align Object properties with primitive types properties like BooleanPropertyor DoubleProperty:

set/setValueget/getValue方法对存在以将对象属性与基本类型属性对齐,例如BooleanPropertyDoubleProperty

BooleanProperty:

布尔属性:

void set(boolean value)
void setValue(java.lang.Boolean v)

DoubleProperty:

双重财产:

void set(double value)
void setValue(java.lang.Number v)

In these property classes ___Valuemethods work with corresponding to type objects while direct methods work with primitive types.

在这些属性类中,___Value方法与类型对象相对应,而直接方法则与原始类型一起工作。

Looking in the code you may find a bit of a difference in the logic. For example, DoubleProperty#setValue(null)is equal to DoubleProperty#set(0.0)(which was required by binding). So generally I'd advise to use set/get methods and leave setValue/getValue to binding needs as they may incorporate additional logic.

查看代码,您可能会发现逻辑上的一些差异。例如,DoubleProperty#setValue(null)等于DoubleProperty#set(0.0)(这是绑定所必需的)。所以通常我建议使用 set/get 方法并将 setValue/getValue 留给绑定需求,因为它们可能包含额外的逻辑。

For Object/String properties there is no difference between set and setValue methods.

对于对象/字符串属性,set 和 setValue 方法之间没有区别。

回答by Alexander Kirov

StringProperty.java :

StringProperty.java :

@Override
public void setValue(String v) {
    set(v);
}

StringPropertyBase.java:

StringPropertyBase.java:

@Override
public void set(String newValue) {
    if (isBound()) {
        throw new java.lang.RuntimeException("A bound value cannot be set.");
    }
    if ((value == null)? newValue != null : !value.equals(newValue)) {
        value = newValue;
        markInvalid();
    }
}

In common case, you can open sources from open javafx and see that.

通常情况下,您可以从 open javafx 打开源代码并查看。