精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>JSP教程>正文

java新手入门:Java反射机制


来源:不详 时间:07-04-25 点击: 点击这里收藏本文
执行结果(例):
public class LinkedList
图5-3:找出class或interface 的名称,及其属性(modifiers)。
 
#001 TypeVariable[] tv;
#002 tv = c.getTypeParameters(); //warning: unchecked conversion
#003 for (int i = 0; i < tv.length; i++) {
#004     x = tName(tv[i].getName(), null); //例如 E,K,V...
#005     if (i == 0) //第一个
#006         System.out.print("<" + x);
#007     else //非第一个
#008         System.out.print("," + x);
#009     if (i == tv.length-1) //最后一个
#010         System.out.println(">");
#011 }
 
执行结果(例):
public abstract interface Map
或 public class LinkedList
图5-4:找出parameterized types 的名称
 
#001 Class supClass;
#002 supClass = c.getSuperclass();
#003 if (supClass != null) //如果有super class
#004     System.out.print(" extends" +
#005 tName(supClass.getName(),classRef));
 
执行结果(例):
public class LinkedList
extends AbstractSequentialList,
图5-5:找出base class。执行结果多出一个不该有的逗号于尾端。此非本处重点,为简化计,不多做处理。
 
#001 Class cc[];
#002 Class ctmp;
#003 //找出所有被实现的interfaces
#004 cc = c.getInterfaces();
#005 if (cc.length != 0)
#006     System.out.print(", \r\n" + " implements "); //关键词
#007 for (Class cite : cc) //JDK1.5 新式循环写法
#008     System.out.print(tName(cite.getName(), null)+", ");
 
执行结果(例):
public class LinkedList
extends AbstractSequentialList,
implements List, Queue, Cloneable, Serializable,
图5-6:找出implemented interfaces。执行结果多出一个不该有的逗号于尾端。此非本处重点,为简化计,不多做处理。
 
#001 cc = c.getDeclaredClasses(); //找出inner classes
#002 for (Class cite : cc)
#003     System.out.println(tName(cite.getName(), null));
#004
#005 ctmp = c.getDeclaringClass(); //找出outer classes
#006 if (ctmp != null)
#007     System.out.println(ctmp.getName());
 
执行结果(例):
LinkedList$Entry
LinkedList$ListItr
图5-7:找出inner classes 和outer class
 
#001 Constructor cn[];
#002 cn = c.getDeclaredConstructors();
#003 for (int i = 0; i < cn.length; i++) {
#004     int md = cn[i].getModifiers();
#005     System.out.print(" " + Modifier.toString(md) + " " +
#006     cn[i].getName());
#007     Class cx[] = cn[i].getParameterTypes();
#008     System.out.print("(");
#009     for (int j = 0; j < cx.length; j++) {
#010         System.out.print(tName(cx[j].getName(), null));
#011         if (j < (cx.length - 1)) System.out.print(", ");
#012     }
#013     System.out.print(")");
#014 }
 
执行结果(例):
public java.util.LinkedList(Collection)
public java.util.LinkedList()
图5-8a:找出所有constructors
 
#004 System.out.println(cn[i].toGenericString());
 
执行结果(例):
public java.util.LinkedList(java.util.Collection)
public java.util.LinkedList()
图5-8b:找出所有constructors。本例在for 循环内使用toGenericString(),省事。
 
#001 Method mm[];
#002 mm = c.getDeclaredMethods();
#003 for (int i = 0; i < mm.length; i++) {
#004     int md = mm[i].getModifiers();
#005     System.out.print(" "+Modifier.toString(md)+" "+
#006     tName(mm[i].getReturnType().getName(), null)+" "+
#007     mm[i].getName());
#008     Class cx[] = mm[i].getParameterTypes();
#009     System.out.print("(");
#010     for (int j = 0; j < cx.length; j++) {
#011         System.out.print(tName(cx[j].getName(), null));
#012     if (j < (cx.length - 1)) System.out.print(", ");
#013     }
#014     System.out.print(")");
#015 }
 
执行结果(例):
public Object get(int)
public int size()
图5-9a:找出所有methods
 
#004 System.out.println(mm[i].toGenericString());
 
public E java.util.LinkedList.get(int)
public int java.util.LinkedList.size()
图5-9b:找出所有methods。本例在for 循环内使用toGenericString(),省事。
 
#001 Field ff[];
#002 ff = c.getDeclaredFields();
#003 for (int i = 0; i < ff.length; i++) {
#004     int md = ff[i].getModifiers();
#005     System.out.println(" "+Modifier.toString(md)+" "+
#006     tName(ff[i].getType().getName(), null) +" "+
#007     ff[i].getName()+";");
#008 }
9 7 3 1 2 4 8 :

  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.