JAVA 反射练习

写一个反射的抽象类。

这个类实现来两个基本的方法,通过属性名赋值和取值。

对属性名和方法有一定的要求,赋值方法必须是set后面跟着首字母大写的字段名...get方法类似...详细看代码。


package com.test.liuzh;

import java.lang.reflect.Method;

public abstract class ExcelDTO {
	@SuppressWarnings("unchecked")
	public  void putValue(String name,Object value){
		Class c = this.getClass();
		Class v = value.getClass();
		try{
			Method m = c.getMethod("set"+name, new Class[]{v});
			m.invoke(this, new Object[]{value});
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unchecked")
	public Object outValue(String name){
		Class c = this.getClass();
		Object o =null;
		try{
			Method m = c.getMethod("get"+name, new Class[]{});
			o = m.invoke(this, new Object[]{});
		}catch(Exception e){
			e.printStackTrace();
		}
		return o;
	}
}

写一个继承类

package com.test.liuzh;

import java.util.Date;

public class TestObj extends ExcelDTO{
	private String name;
	private Integer age;
	private Date birthday;
	private String address;
	private String email;
	private String phone;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return name+","+age+","+email+","+phone+","+address;
	}
}

写测试类

package com.test.liuzh;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class RefleTest {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		//模拟获取的excel数据
		List<String> list = new ArrayList();
		List<HashMap<String,Object>> list2 = new ArrayList();
		list.add("Name");
		list.add("Age");
		list.add("Birthday");
		list.add("Email");
		list.add("Phone");
		list.add("Address");
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("Name", "徐长今");
		list2.add(map);
		map = new HashMap<String,Object>();
		map.put("Age", 16);
		list2.add(map);
		map = new HashMap<String,Object>();
		map.put("Birthday", new Date());
		list2.add(map);
		map = new HashMap<String,Object>();
		map.put("Email", "changjin@hanguo.com");
		list2.add(map);
		map = new HashMap<String,Object>();
		map.put("Phone", "13312312301");
		list2.add(map);
		map = new HashMap<String,Object>();
		map.put("Address", "首尔");
		list2.add(map);
		
		
		
		//第一种方式
		TestObj o =  new TestObj();
		o.putValue("Name", "林晚荣");
		o.putValue("Email", "abc@125.com");
		o.putValue("Phone", "120807756");
		o.putValue("Address", "地址");
		o.putValue("Age", 24);
		o.putValue("Birthday", new Date());
		System.out.println(o.toString());
		
		//第二种方式
		o=new TestObj();
		for(int i=0;i<list2.size();i++){
			HashMap<String,Object> hm = list2.get(i);
			//存入类中
			o.putValue(list.get(i), hm.get(list.get(i)));
		}
		System.out.println(o.toString());
		
		
		
		//获取值
		for(int i=0;i<list.size();i++){
			System.out.print(list.get(i)+":"+o.outValue(list.get(i))+"   ");
		}
	}
}

输出结果:

林晚荣,24,abc@125.com,120807756,地址
徐长今,16,changjin@hanguo.com,13312312301,首尔
Name:徐长今   Age:16   Birthday:Thu Mar 15 22:41:15 CST 2012   Email:changjin@hanguo.com   Phone:13312312301   Address:首尔   

这只是初步了解反射...稍后慢慢熟悉更多特性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isea533

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值