第三十一章反射入門技術
反射を利用して一例を作ったようで、newを使わないで、具体的な作用は分かりません
package cn.itcast.jdbc;
import java.lang.reflect.Constructor;
public class ReflectTest {
public static void main(String[] args)throws Exception{
Class clazz = User.class;
clazz = Bean.class;
Object obj = create(clazz);
System.out.println(obj);
}
static Object create(Class clazz)throws Exception{
Constructor con = clazz.getConstructor(String.class);
Object obj = con.newInstance("test name");
return obj;
}
}
package cn.itcast.jdbc;
import java.sql.Date;
public class User {
private Integer id;
private String name;
private Date birthday;
private Float money;
public User(String name){
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
}
package cn.itcast.jdbc;
public class Bean {
private String name;
public Bean(String n){
this.name = name;
}
public String toString(){
return super.toString();
}
}