对于 JavaBean 属性的 getter 和 setter 命名感到困惑
我正在制作一个使用 Drools planner 的应用程序。
@ValueRangeFromSolutionProperty
应该引用另一个类的属性(在本例中为 NQueens
)。来自 @ValueRangeFromSolutionProperty的JavaDocs:
propertyName
The property name of which exists a getter on the Solution that returns a Collection.
但我注意到一个不一致:注释器使用来自 NQueens
的属性 rowList
。但 rowList
(与 RowList
相反)是一个私有变量(请参见下面的代码片段)。如果它应该通过内省推断属性(从它的 getter 和 setter 方法),那么它不应该像 getRowList()
中那样拼写为 RowList
?
问题:Java 如何从 getter 方法推断(内省)属性名称(大小写和全部)?
或者@ValueRangeFromSolutionProperty
是否直接访问私有变量?
背景详情: 来自 Queen.java
,一个表示棋盘上的皇后的类:
public class Queen extends AbstractPersistable {
....
@ValueRangeFromSolutionProperty(propertyName = "rowList")
public Row getRow() {
return row;
....
来自 NQueens.java
,@ValueRangeFromSolutionProperty
从中获取它的类财产来自:
public class NQueens extends AbstractPersistable implements Solution<SimpleScore> {
...
private List<Column> columnList;
private List<Row> rowList;
....
public List<Row> getRowList() {
return rowList;
...
I am making an application that uses Drools planner.
The @ValueRangeFromSolutionProperty
is supposed to refer to a property from another class (NQueens
in this case). From the JavaDocs for @ValueRangeFromSolutionProperty:
propertyName
The property name of which exists a getter on the Solution that returns a Collection.
But I noticed an inconsistency: the annotator uses the property rowList
from NQueens
. But rowList
(as opposed to RowList
) is a private variable (see snippets below). If it were supposed to infer a property by introspection (from it's getter and setter methods), shouldnt it be spelled RowList
as in getRowList()
?
Question: How does Java infer (introspect) the property name (case and all) from the getter methods?
Or does the @ValueRangeFromSolutionProperty
access the private variables directly?
Background details:
From Queen.java
, a class that represents a queen on a chessboard:
public class Queen extends AbstractPersistable {
....
@ValueRangeFromSolutionProperty(propertyName = "rowList")
public Row getRow() {
return row;
....
From NQueens.java
, the class from which the @ValueRangeFromSolutionProperty
gets it's property from:
public class NQueens extends AbstractPersistable implements Solution<SimpleScore> {
...
private List<Column> columnList;
private List<Row> rowList;
....
public List<Row> getRowList() {
return rowList;
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JavaBeans 规范 表示对于属性
propertyName
应该有一个 getter 方法getPropertyName()
和/或一个 setter 方法setPropertyName()
。属性仅由 getter 和 setter 方法定义,也可以是计算值。对象上不需要实例变量。
该规范定义了属性和 getter/setter 方法的大小写规则:
该方法实际上实现为:
因此:
name
为 null,则返回 如果name
的前两个字符大写The JavaBeans Specification says that for a property
propertyName
there should be a getter methodgetPropertyName()
and/or a setter methodsetPropertyName()
.A property is defined by the only presence of the getter and setter methods and can also be a computed value. A instance variable on the object is not required.
The specification defines the capitalization rules for properties and getter/setter methods:
The method is in fact implemented as:
So:
name
is null, return it as suchname
has first two characters in caps, return it as such这是由 JavaBeans 命名约定定义的。 getter 名称将包含“get”,后跟第一个字母大写的属性名称。
包含更多信息的相关问题
That's defined by the JavaBeans naming conventions. The getter name will have "get" followed by the property name with the first letter capitalized.
A related question with more information
当 javabean 引用“属性”时,它是带有 get() 和 set() 方法的东西。它不关心数据的内部存储是什么(如果有的话)。
因此,属性“foo”具有访问方法 getFoo() 和 setFoo(),这些方法的作用与属性的定义无关。
http://docs.oracle.com/javase/tutorial/javabeans/writing /properties.html
When javabeans refer to a "property" it is something with a get() and a set()-method. It doesn't care what the internal storage of data is (if there even is one).
Thus a property "foo" has access methods getFoo() and setFoo(), what these methods do is irrelevant to the definition of the property.
http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html