欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 类反射 练习

程序员文章站 2024-03-15 17:46:06
...

java 类反射 练习

package cn.hncu.reflect.ex;

public class User {
	private String id;
	private String name;//setName
	private int age;//setAge
	public User(String id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public User() {
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	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;
	}
	
	@Override
	public String toString() {
		return id + ", " + name + ", " + age;
	}
	
}

import java.io.Serializable;

public class BookModel implements Serializable{
	private String uuid;
	private String name;
	private double inPrice;
	private double salePrice;
	
	public BookModel() {
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getInPrice() {
		return inPrice;
	}

	public void setInPrice(double inPrice) {
		this.inPrice = inPrice;
	}

	public double getSalePrice() {
		return salePrice;
	}

	public void setSalePrice(double salePrice) {
		this.salePrice = salePrice;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BookModel other = (BookModel) obj;
		if (uuid == null) {
			if (other.uuid != null)
				return false;
		} else if (!uuid.equals(other.uuid))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return uuid + ",《" + name + "》,进价:"
				+ inPrice + ",售价:" + salePrice + "元";
	}
	
}
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

public class MyBeanUtils {
	
	public static<T> T populate(Class<T> c, Map<String,Object> map) throws Exception{
		//1通过类模板c创建一个空对象obj
		T obj = c.newInstance();
		
		/*2 遍历类模板c中的所有属性,分别把这些属性当作key到map中
		 * 去取value,若有值则调用对应的setter方法进行封装
		 */
		Field flds[] = c.getDeclaredFields();
		if(flds==null){
			return obj;
		}
		for(Field fld: flds){
			Object value = map.get( fld.getName() );
			if(value==null){
				System.out.println("属性"+fld.getName()+"的数据为空,没有封装上");
				continue;
			}
			
			//value不为空时
			//生成对应属性的setter方法名="set"+首字母大写+name.subString(1)
			String methodName = "set" + fld.getName().substring(0, 1).toUpperCase() + fld.getName().substring(1);
			Method m = c.getMethod( methodName, fld.getType() );
			m.invoke(obj,value);
		}
		
		//3 把封装完成的对象obj返回
		
		return obj;
	}
	
}
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class BeanUtilsTest {
	@Test
	public void t1() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		//map.put("id","A001");
		map.put("name","Jack");
		map.put("price",12.45);
		map.put("age",23);
		User u = MyBeanUtils.populate(User.class, map);
		System.out.println(u);
	}
	@Test
	public void t2() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id","A001");
		map.put("name","三国演义");
		map.put("inPrice",12.45);
		map.put("age",23);
		BookModel u = MyBeanUtils.populate(BookModel.class, map);
		System.out.println(u);
	}
}