问题描述
我在Java中使用了一些我并不理解的功能,因此我想阅读它以便我可以更有效地使用它。问题是我不知道它叫什么,因此很难获得更多信息:
I'm using some functionality in Java that I don't really understand so I want to read up on it so that I can use it more effectively. The problem is that I don't know what it is called so it makes it difficult to get more information on it:
我有一个班级 Foo
定义如下:
I have a class Foo
defined like this:
private String _name;
private Bar _bar;
//getters and setters
和 Bar
:
private String _code;
//getters and setters
public String get_isCodeSmith()
{
boolean rVal = _code.toLowerCase().contains("smith");
return rVal;
}
不知何故,在我的JSP页面中(当我有会话变量名为 Foo
)我能够编写如下逻辑标记:
Somehow, in my JSP pages (when I have a Session
variable called Foo
) I am able to write logic tags like this:
<logic:equal name="Foo" property="_bar._isCodeSmith" value="true">
即使没有属性 _isCodeSmith
在我的班级 Bar
中,它会自动运行 get_isCodeSmith()
方法。
And even though there is no attribute _isCodeSmith
in my class Bar
, it runs the get_isCodeSmith()
method automatically.
这叫什么,我在哪里可以找到更多?
What is this called and where can I find out more?
推荐答案
这是。属性不是由字段标识,而是由getter(访问器)和/或setter(mutator)方法标识。
This is the Javabeans mechanism. Properties are identified not by fields, but by getter (accessor) and / or setter (mutator) methods.
有关更多技术信息,请阅读
For more technical info, read the JavaBeans spec
或者看看这个简单的测试类:
Or have a look at this simple test class:
public class TestBean {
private String complete;
public String getComplete() { return complete; }
public void setComplete(final String complete) { this.complete = complete; }
private String getterOnly;
public String getGetterOnly() { return getterOnly; }
private String setterOnly;
public void setSetterOnly(final String setterOnly) { this.setterOnly = setterOnly; }
public String getNoBackingField() { return ""; }
}
和简单的JavaBeans分析:
and the simple JavaBeans analysis:
public class Test {
public static void analyzeBeanProperties(final Class<?> clazz) throws Exception {
for (final PropertyDescriptor propertyDescriptor
: Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors()) {
System.out.println("Property name: " + propertyDescriptor.getName());
System.out.println("Getter method: " + propertyDescriptor.getReadMethod());
System.out.println("Setter method: " + propertyDescriptor.getWriteMethod());
System.out.println();
}
}
public static void main(final String[] args) throws Exception {
analyzeBeanProperties(TestBean.class);
}
}
输出
Property name: complete
Getter method: public java.lang.String test.bean.TestBean.getComplete()
Setter method: public void test.bean.TestBean.setComplete(java.lang.String)
Property name: getterOnly
Getter method: public java.lang.String test.bean.TestBean.getGetterOnly()
Setter method: null
Property name: noBackingField
Getter method: public java.lang.String test.bean.TestBean.getNoBackingField()
Setter method: null
Property name: setterOnly
Getter method: null
Setter method: public void test.bean.TestBean.setSetterOnly(java.lang.String)
这篇关于Java getter用于类的不存在的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!