我有一个类,它有一些SimpleStringProperty类型的变量。当使用这样的无参数构造函数初始化此类时:Foo f = new Foo();
,则不初始化SimpleStringProperty变量。因此,我必须像这样初始化该实例:Foo f = new Foo(null, null);
但是,有没有其他简单/干净的方法来初始化这些类的实例?我也试过像这样扩展这个类:
public class Foo extends StringPropertyBase{//...}
,
但仍然没有参数构造函数不初始化变量。
Foo.java
public class Foo{
private SimpleStringProperty x;
private SimpleStringProperty y;
public Foo(String a, String b){
this.x = new SimpleStringProperty(a);
this.x = new SimpleStringProperty(b);
}
public StringProperty xProperty(){return x;}
public StringProperty yProperty(){return y;}
//...
}
答案 0 :(得分:0)
你不能只做这样的事情,将它们初始化为空字符串""
吗?
public Foo() {
this.x = new SimpleStringProperty("");
this.y = new SimpleStringProperty("");
}
显然,这可以改为提供你想要的任何理智的默认值。
然后可以通过绑定到它的任何东西在另一个时间更改属性,或者通过提供允许更新属性的setter来更改属性,如下所示:
public void setX(String value) {
this.xProperty().set(value);
}