JavaBeans:属性和特性之间有什么区别?

标签 java properties attributes javabeans

在我的修订列表的 JavaBean 部分中,它声明我应该知道“属性和特性之间的区别”。我真的找不到两者之间的区别。我知道 JavaBeans 使用属性,而普通 Java 类使用属性(或者至少我被教导如何调用它们),但我看不出真正的区别。

与getter/setter方法有关吗?

谢谢

最佳答案

例子

属性和属性是等价的

private int age;

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

属性age转化为personAge属性

private int personAge;

public int getAge() {
    return personAge;
}

public void setAge(int age) {
    this.personAge = age;
}

属性是合成的,没有属性

在这种情况下,该属性是只读的:

private int age;
private Sex sex;

public boolean isFemaleAdult() {
    return sex == Sex.FEMALE && age >= 18
}

我在 Tapestry documentation 中发现了一些有趣的提示:

A property is not the same as an attribute ... though, most often, each property is backed up by an attribute.

及以后:

Another common pattern is a synthesized property. Here, there is no real attribute at all, the value is always computed on the fly.

关于JavaBeans:属性和特性之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773993/

相关文章:

c++ - 将自定义属性分组到 Maya 中的节点

python - 属性错误: 'Ball' object has no attribute 'x'

java - Android 应用程序中的 NumberFormatException

java - GCP 发布订阅 :Creating a Configuration Class to publish message

java - EL1012E : Cannot index into a null value

c# - 使用反射获取baseclass属性值-与this.Property相反的base.Property

javascript - 我如何获得原始值的引用?

c# - 如何使用 GetType GetValue 区分两个对象的属性值?

java - 如何将帕斯卡三角生成器从二维数组转换为一维数组?

jquery - 如何在不是类或 id 的属性上使用 jQuery filter()