什么是 IntegerProperty 以及为什么需要导入它
what is IntegerProperty and why does it need to be imported
所以在class中我们总是使用下面的语法。如果我错了请纠正我,但这是一个 bean,因为它 class 使用 getters/setters。它有一个 nullary 构造函数和 class 实现可序列化。
// option 1
private int customerID ;
public CustomerDTO ()
{
this(0);
}
public CustomerDTO(int customerID) {
setCustomerID(customerID);
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public int getCustomerID() {
return customerID;
}
但是今天我遇到了类似的事情。我需要导入
import javafx.beans.property.SimpleStringProperty;
但是选项 1 和选项 2 之间的主要区别是什么。
我应该什么时候使用选项 1 或选项 2
到底哪个好还是要看情况。
// option 2
private final IntegerProperty customerID;
public CustomerDTO ()
{
this(null);
}
public CustomerDTO(IntegerProperty customerID) {
this.customerID = new SimpleIntegerProperty();
}
public IntegerProperty getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID.set(customerID);
}
选项 2 在您构建 JavaFX 应用程序并希望将您的模型与 gui 绑定时使用。
示例:
public class Foo {
private final StringProperty foo = new SimpleStringProperty();
public String getFoo() {
return foo.get();
}
public StringProperty fooProperty() {
return foo;
}
public void setFoo(String foo) {
this.foo.set(foo);
}
}
public class FooController {
@FXML
private TextField fooTextField;
private final Foo foo = new Foo();
@FXML
public void initialize() {
foo.fooProperty().bindBidirectional(fooTextField.textProperty());
}
}
public CustomerDTO(IntegerProperty customerID) {
没有意义,一个property
是一个final
class成员封装了一个value
,这个值可以通过setter和通过 setter 获取,在 JavaFX 控制器 classes 中,建议还实现一个 getter 因为 ReadOnlyObjectProperty or ReadOnlyIntegerProperty in your case, this can be done via ReadOnlyIntegerWrapper and its getReadOnlyProperty method. This enables the developor to bind to values from other classes whilst also ensuring that the value exists at any time, JavaFX Bindings 是一种非常优雅且面向对象的数据封装方法。
您的 "option 2" 实际上是有缺陷的,因为它允许 属性-重新定义,这打破了这个概念并使 属性 本身变得无用。它还会破坏 GUI 功能,除非 属性 本身无法重新定义,请参阅已接受的答案
所以在class中我们总是使用下面的语法。如果我错了请纠正我,但这是一个 bean,因为它 class 使用 getters/setters。它有一个 nullary 构造函数和 class 实现可序列化。
// option 1
private int customerID ;
public CustomerDTO ()
{
this(0);
}
public CustomerDTO(int customerID) {
setCustomerID(customerID);
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public int getCustomerID() {
return customerID;
}
但是今天我遇到了类似的事情。我需要导入
import javafx.beans.property.SimpleStringProperty;
但是选项 1 和选项 2 之间的主要区别是什么。 我应该什么时候使用选项 1 或选项 2 到底哪个好还是要看情况。
// option 2
private final IntegerProperty customerID;
public CustomerDTO ()
{
this(null);
}
public CustomerDTO(IntegerProperty customerID) {
this.customerID = new SimpleIntegerProperty();
}
public IntegerProperty getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID.set(customerID);
}
选项 2 在您构建 JavaFX 应用程序并希望将您的模型与 gui 绑定时使用。
示例:
public class Foo {
private final StringProperty foo = new SimpleStringProperty();
public String getFoo() {
return foo.get();
}
public StringProperty fooProperty() {
return foo;
}
public void setFoo(String foo) {
this.foo.set(foo);
}
}
public class FooController {
@FXML
private TextField fooTextField;
private final Foo foo = new Foo();
@FXML
public void initialize() {
foo.fooProperty().bindBidirectional(fooTextField.textProperty());
}
}
public CustomerDTO(IntegerProperty customerID) {
没有意义,一个property
是一个final
class成员封装了一个value
,这个值可以通过setter和通过 setter 获取,在 JavaFX 控制器 classes 中,建议还实现一个 getter 因为 ReadOnlyObjectProperty or ReadOnlyIntegerProperty in your case, this can be done via ReadOnlyIntegerWrapper and its getReadOnlyProperty method. This enables the developor to bind to values from other classes whilst also ensuring that the value exists at any time, JavaFX Bindings 是一种非常优雅且面向对象的数据封装方法。
您的 "option 2" 实际上是有缺陷的,因为它允许 属性-重新定义,这打破了这个概念并使 属性 本身变得无用。它还会破坏 GUI 功能,除非 属性 本身无法重新定义,请参阅已接受的答案