在我的修订列表的 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/