您的位置:首页 > 编程语言 > Java开发

JAVA 反射练习

2012-03-15 22:46 323 查看
写一个反射的抽象类。

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

对属性名和方法有一定的要求,赋值方法必须是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:首尔   

这只是初步了解反射...稍后慢慢熟悉更多特性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息