最近在学习java一些高大上的技术,了解到java中反射技术的重要。开始对反射技术进行学习,也算是略懂皮毛。
接下来我就来讲一讲我的学习,最好将用到的方法封装成公用方法BeanUtils类,生命一些静态方法。我使用反射封装一些小方法,是为了对大部分有共性的方法通用,所以要求,
1、Bean类的属性符合属性命名规则,首字母小写而且必须有getXXX与setXXX方法;
2、属性xxx对应的getXxx,setXxx,Xxx必须与类的属性字段名一致,而首字母要大写;
3、getXxx是无参的,setXxx参数只能有1个,而且类型必须与属性一致(除了基础数据类型,他们可以自动打包);
public class Person {
private String name;
private int age;
private Date birthday;
public Person() {
}
public Person(String name, int age, Date birthday) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}