注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

JAVA的反射机制简介(下)  

2012-01-13 14:20:10|  分类: JAVA基础 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
以下只是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

  评论这张
 
阅读(594)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017