JAVA的反射机制简介(下)

来源:互联网 发布:哪里有专业的java培训 编辑:程序博客网 时间:2024/06/03 23:47
以下只是JAVA的反射机制的一简单示例,关于其理论在知识和API的详细介绍请阅读《JAVA的反射机制简介(上)
代码
package com.teleca.robin;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.Date;

public class ReflectDemo {
void start() {

Class<?> classType = People.class;
System.out.println("-----getFields------");
// 使用getFields获取属性
Field[] fields = classType.getFields();
for (Field f : fields) {
System.out.println(f);
}

System.out.println("--------getDeclaredFields--------");

// 使用getDeclaredFields获取属性
fields = classType.getDeclaredFields();
for (Field f : fields) {
System.out.println(f);
}
System.out.println("------getMethod-----");
// 使用getMehods获取属性
Method methods[] = classType.getMethods();
for (Method m :methods) {
System.out.println(m);
}

System.out.println("-------getDeclaredMethod------");

// 使用getDeclaredFields获取属性
methods = classType.getDeclaredMethods();
for (Method m :methods) {
System.out.println(m);
}
try {
Class<?> c = Class.forName("com.teleca.robin.People");
Method method = c.getMethod("getBornDate");
Object t = c.newInstance();
Object d = method.invoke(t);
System.out.println("BornDate:" + d);
Date myDate = (Date) d;
System.out.println("myDate:" + myDate);
Constructor<?> constructor2 = c.getDeclaredConstructor(int.class,String.class,Date.class);
t =constructor2.newInstance(1,"robin",myDate);
People p=(People)t;
d = method.invoke(t);
myDate = (Date) d;
System.out.println(p.name+"'BornDate is " + myDate);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}
class People implements Comparator<People>
{
private int id;
public String name;
private int age;
Date date;
static Date curDate=new Date();
 public People()
 {
 id=-1;
 }
 public People(int id,String name,Date birthDate)
 {
 this.id=id;
 this.name=name;
 countAge();
 }
public Date getBornDate() {
return date;
}
public static boolean isYoung(People p)
{
return p.age<40;
}
public void setName(String name)
{
if(id!=-1)
throw new UnsupportedOperationException("when id is not -1,you can not change name");
this.name=name;
}
public void setBirthDate(Date date)
{
if(id!=-1)
throw new UnsupportedOperationException("when id is not -1,you can not change name");
this.date=date;
}
private void countAge()
{
this.age=(curDate.getYear())-date.getYear();
}
@Override
public int compare(People p0, People p1) {
// TODO Auto-generated method stub
if(p0.id==p1.id)
{
return 0;
}
else if(p0.id<p1.id)
{
return -1;
}
return 1;
}
}
运行结果:
-----getFields------
public java.lang.String com.teleca.robin.People.name
--------getDeclaredFields--------
private int com.teleca.robin.People.id
public java.lang.String com.teleca.robin.People.name
private int com.teleca.robin.People.age
java.util.Date com.teleca.robin.People.date
static java.util.Date com.teleca.robin.People.curDate
------getMethod-----
public java.util.Date com.teleca.robin.People.getBornDate()
public static boolean com.teleca.robin.People.isYoung(com.teleca.robin.People)
public void com.teleca.robin.People.setBirthDate(java.util.Date)
public int com.teleca.robin.People.compare(java.lang.Object,java.lang.Object)
public int com.teleca.robin.People.compare(com.teleca.robin.People,com.teleca.robin.People)
public void com.teleca.robin.People.setName(java.lang.String)
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
-------getDeclaredMethod------
public java.util.Date com.teleca.robin.People.getBornDate()
private void com.teleca.robin.People.countAge()
public static boolean com.teleca.robin.People.isYoung(com.teleca.robin.People)
public void com.teleca.robin.People.setBirthDate(java.util.Date)
public int com.teleca.robin.People.compare(java.lang.Object,java.lang.Object)
public int com.teleca.robin.People.compare(com.teleca.robin.People,com.teleca.robin.People)
public void com.teleca.robin.People.setName(java.lang.String)
BornDate:Mon Feb 13 11:07:24 GMT+08:00 2012
myDate:Mon Feb 13 11:07:24 GMT+08:00 2012
robin'BornDate is Mon Feb 13 11:07:24 GMT+08:00 2012

原创粉丝点击