java - SimpleProperties 用法

标签 java properties

在 Java FX 应用程序中使用属性 我开始质疑对属性用法的理解:使用类实例成员 ...Property 与 Simple...Property 的实际区别是什么?

public class Foo {

    private IntegerProperty id;

    public Foo(int id) {
        this.id = new SimpleIntegerProperty(id);
    }

    public IntegerProperty idProperty() {
        return this.id;
    }

    public int getId() {
        return id.get();
    }

    public void setId(int id) {
         this.id.set(id);
    }
}

反对

public class Foo {

    private SimpleIntegerProperty id;

    public Foo(int id) {
        this.id = new SimpleIntegerProperty(id);
    }

   public SimpleIntegerProperty idProperty() {
       return this.id;
   }

   public int getId() {
       return id.get();
   }

    public void setId(int id) {
       this.id.set(id);
   }
}

最佳答案

您的第一个实现非常好。

您绝对应该尽可能使用最抽象的类型(在您的情况下为 IntegerProperty)。

主要原因是它允许您更改实现而无需更改方法的原型(prototype)/定义,因此无需更改任何调用方。

考虑与 Set 相同的情况,并从 HashSet 具体类型迁移到 LinkedHashSet。

关于java - SimpleProperties 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53890520/

相关文章:

java - 为什么要在包上使用许多子项目和依赖项?

asp.net - 对于 HiddenField,Visible 属性有什么用?

java - 耶拿 - 如何将资源链接到属性?

java - Web 应用程序中的属性文件

java - Spring JPA : ManyToMany relationship not stored in database when reuse the property to store another object

java - 合并多个 if 语句

java - 我无法可视化位于 java 容器中的 Oracle 库中的表

java - 如何在 Tomcat/linux 服务器上配置 kerberos?

java - Web 服务器属性加载

windows - 文件的获取日期