- import java.beans.BeanInfo;
- import java.beans.IntrospectionException;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.InvocationTargetException;
- import java.util.Date;
- import org.apache.commons.beanutils.BeanUtils;
- /**
- * @author Shawn
- *
- */
- class Heart{
- private String left;
- private String right;
- public String getLeft() {
- return left;
- }
- public void setLeft(String left) {
- this.left = left;
- }
- public String getRight() {
- return right;
- }
- public void setRight(String right) {
- this.right = right;
- }
-
- }
- class Person{
- private Heart heart;
- private Date birthday;
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Heart getHeart() {
- return heart;
- }
- public void setHeart(Heart heart) {
- this.heart = heart;
- }
- }
- public class BeanUtilsTest2 {
- /**
- * @param args
- * @throws NoSuchMethodException
- * @throws InvocationTargetException
- * @throws IllegalAccessException
- */
- public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
- // TODO Auto-generated method stub
- Person p = new Person();
- System.out.println(BeanUtils.getProperty(p, "heart"));
- }
- }
复制代码 这段代码的错误信息说heart没有对应的get和set方法,让我很纳闷,后来把Heart和Person类都单独放到一个java文件中,用public声明,就可以了,不管是heart还是heart.left这种级联操作,都可以,所以,是不是JavaBean的类需要用public修饰才可以?
|