关闭

第三十一章 反射入门技术

195人阅读 评论(0) 收藏 举报

貌似就是利用反射构造出一个实例出来,而没有用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();
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论