最后生成的实体打印出来是null,这是为啥?(代码如下:)import java.lang.reflect.Method;
import java.util.Date;public class ReflectTest {
private Integer id;
private String userName;
private String passWord;
private Date birchday;
public static void main(String[] args) throws Exception {
ReflectTest rt = null;
String[] getM = new String[4];
String[] setM = new String[4];
String[] attributeNames = new String[]{"id","userName","passWord","birchday"};
for (int i = 0; i < 4; i++) {
String a = Character.toUpperCase(attributeNames[i].charAt(0)) + attributeNames[i].substring(1);
getM[i] = "get" + a;
setM[i] = "set" + a;
//System.out.println(methodNames[i]);
rt = new ReflectTest();
Method get = rt.getClass().getMethod(getM[i]);
Method set = rt.getClass().getMethod(setM[i],get.getReturnType());
//System.out.println(set.getName());
//System.out.println(get.getReturnType().getName());
if (get.getReturnType().getName().equals("java.lang.String")) {
set.invoke(rt, "my" + attributeNames[i]);
}
}
System.out.println(rt);
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWord() {
return passWord;
} public void setPassWord(String passWord) {
this.passWord = passWord;
} public Date getBirchday() {
return birchday;
} public void setBirchday(Date birchday) {
this.birchday = birchday;
} @Override
public String toString() {
return "ReflexTest [birchday=" + birchday + ", id=" + id
+ ", passWord=" + passWord + ", userName=" + userName + "]";
}
}
import java.util.Date;public class ReflectTest {
private Integer id;
private String userName;
private String passWord;
private Date birchday;
public static void main(String[] args) throws Exception {
ReflectTest rt = null;
String[] getM = new String[4];
String[] setM = new String[4];
String[] attributeNames = new String[]{"id","userName","passWord","birchday"};
for (int i = 0; i < 4; i++) {
String a = Character.toUpperCase(attributeNames[i].charAt(0)) + attributeNames[i].substring(1);
getM[i] = "get" + a;
setM[i] = "set" + a;
//System.out.println(methodNames[i]);
rt = new ReflectTest();
Method get = rt.getClass().getMethod(getM[i]);
Method set = rt.getClass().getMethod(setM[i],get.getReturnType());
//System.out.println(set.getName());
//System.out.println(get.getReturnType().getName());
if (get.getReturnType().getName().equals("java.lang.String")) {
set.invoke(rt, "my" + attributeNames[i]);
}
}
System.out.println(rt);
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWord() {
return passWord;
} public void setPassWord(String passWord) {
this.passWord = passWord;
} public Date getBirchday() {
return birchday;
} public void setBirchday(Date birchday) {
this.birchday = birchday;
} @Override
public String toString() {
return "ReflexTest [birchday=" + birchday + ", id=" + id
+ ", passWord=" + passWord + ", userName=" + userName + "]";
}
}
解决方案 »
- 一个C#项目,想要的结果没有出来,帮忙看看。
- 急,哪里出错!!!!
- 如何实现简单的http服务器,使其可以收发SOAP协议包??
- 一个基础问题,望高手解答。谢谢!
- 没有循环的小应用程序怎么会重复执行paint()方法?
- JAVA中是否有内存泄漏?菜鸟问题
- 看过<<CORE JAVA>>的朋友进来帮个忙,有个例题搞不懂?
- 欢迎朋友们加入开放源代码工程 防火洞!!
- 一个JAVA问题,事件监听接口怎么不能编译,但是去掉就可以了呢?(我必须要这个监听接口,怎样解决)
- 关于javadoc
- 求助Java用SWT的Text做一个类似Win下的IP输入框,如何限制其只能输入数字
- 使用字符流完成java socket编程
提练一下问题,代码不用全发,像什么 set get的拿掉就行。
Method get = rt.getClass().getMethod(getM[i]);
Method set = rt.getClass().getMethod(setM[i],get.getReturnType());
给你贴一个可以跑的反射代码
import java.lang.reflect.*;public class ReflectTester {
public Object copy(Object object)throws Exception{
Class classType=object.getClass();
System.out.println("class: "+classType.getName());
Object objectCopy=classType.getConstructor(new Class[]{}).
newInstance(new Object[]{});//需要研究一下,这里先创建一个实例
Field fields[]=classType.getDeclaredFields();
for(int i=0;i<fields.length;i++){
Field field=fields[i];
String fieldName=field.getName();
String firstLetter=fieldName.substring(0,1).toUpperCase();
String getMethodName="get"+firstLetter+fieldName.substring(1);
String setMethodName="set"+firstLetter+fieldName.substring(1);
Method getMethod=classType.getMethod(getMethodName, new Class[]{});
Method setMethod=classType.getMethod(setMethodName, new Class[]{field.getType()});
Object value=getMethod.invoke(object, new Object[]{});
System.out.println(fieldName+":"+value);
setMethod.invoke(objectCopy, new Object[]{value});
}
return objectCopy;
} /**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Customer customer=new Customer("Tom",21);
customer.setId(new Long(1L));
Customer customerCopy=(Customer)new ReflectTester().copy(customer);
System.out.println("Copy information:"+customerCopy.getName()+" "+
customer.getAge());
}}class Customer{
private long id;
private String name;
private int age;
public Customer(){}
public Customer(String name,int age){
this.name=name;
this.age=age;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
为什么在循环里new ,外面去。
private Integer id;
private String userName;
private String passWord;
private Date birchday; public static void main(String[] args) throws Exception {
ReflectTest rt = new ReflectTest();
String[] getM = new String[4];
String[] setM = new String[4];
String[] attributeNames = new String[]{"id","userName","passWord","birchday"};
for (int i = 0; i < 4; i++) {
String a = Character.toUpperCase(attributeNames[i].charAt(0)) + attributeNames[i].substring(1);
getM[i] = "get" + a;
setM[i] = "set" + a;
//System.out.println(methodNames[i]);
Method get = rt.getClass().getMethod(getM[i],new Class[]{});
Method set = rt.getClass().getMethod(setM[i],new Class[]{rt.getClass().getDeclaredField(attributeNames[i]).getType()});
//System.out.println(set.getName());
//System.out.println(get.getReturnType().getName());
if (get.getReturnType().getName().equals("java.lang.String")) {
System.out.println("set Method name: " + set.getName());
set.invoke(rt, new String[]{"my" + attributeNames[i]});
}
}
System.out.println(rt);
}
}这里省略了其他部分。另外,为什么楼主那里输出是空的,除了编译不通过的原因之外,楼主是在每次循环的时候重新初始化了一次这个rt,并且最后没有执行任何的set方法,所以输出出来是空的。如果解决了楼主的问题麻烦给分吧,谢谢啊!!!