详细介绍java的反射技术

反射的定义:审查元数据并收集关于它的类型信息的能力。下面介绍java的反射技术。

按需规划网站可以根据自己的需求进行定制,成都做网站、成都网站建设构思过程中功能建设理应排到主要部位公司成都做网站、成都网站建设的运用实际效果公司网站制作网站建立与制做的实际意义

Lesson: 检测类examing class

1.Retrieving Class Objects

获取一个Class对象(metadata)

a,从对象的实例获取。

 
 
  1. Class c = mystery.getClass();//(return Class) 

b,从子类的实例获取

 
 
  1. TextField t = new TextField();   
  2. Class c = t.getClass();   
  3. Class s = c.getSuperclass(); 

c,知道类名,则可以把.class加入到名字之后来获取。

 
 
  1. Class c = java.awt.Button.class; 

d,如果类名在编译时是未知的,则可以使用Class.forName()方法来获取.

 
 
  1. Class c = Class.forName(classString); 

2.Getting the Class Name

获取类名称

 
 
  1. c.getName(); 

例如:

 
 
  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleName {  
  4. public static void main(String[] args) {  
  5. Button b = new Button();  
  6. printName(b);  
  7. }  
  8. static void printName(Object o) {  
  9. Class c = o.getClass();  
  10. String s = c.getName();  
  11. System.out.println(s);  
  12. }  

3.Discovering Class Modifiers

检索修改符

a.通过getModifiers()方法获取一个整型标识值。

b.通过java.reflect.Modifier对象的isPublic, isAbstract, 和 isFinal方法判断此值.

例如:

 
 
  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleModifier {  
  4. public static void main(String[] args) {  
  5. String s = new String();  
  6. printModifiers(s);  
  7. }  
  8. public static void printModifiers(Object o) {  
  9. Class c = o.getClass();  
  10. int m = c.getModifiers();  
  11. if (Modifier.isPublic(m))  
  12. System.out.println("public");  
  13. if (Modifier.isAbstract(m))  
  14. System.out.println("abstract");  
  15. if (Modifier.isFinal(m))  
  16. System.out.println("final");  
  17. }  

4.Finding Superclasses

检索父类

例如:

 
 
  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleSuper {  
  4. public static void main(String[] args) {  
  5. Button b = new Button();  
  6. printSuperclasses(b);  
  7. }  
  8. static void printSuperclasses(Object o) {  
  9. Class subclass = o.getClass();  
  10. Class superclass = subclass.getSuperclass();  
  11. while (superclass != null) {  
  12. String className = superclass.getName();  
  13. System.out.println(className);  
  14. subclass = superclass;  
  15. superclass = subclass.getSuperclass();  
  16. }  
  17. }  

5.Identifying the Interfaces Implemented by a Class

检索指定类实现的接口

例如:

 
 
  1. import java.lang.reflect.*;  
  2. import java.io.*;  
  3. class SampleInterface {  
  4. public static void main(String[] args) {  
  5. try {  
  6. RandomAccessFile r = new RandomAccessFile("myfile", "r");  
  7. printInterfaceNames(r);  
  8. } catch (IOException e) {  
  9. System.out.println(e);  
  10. }  
  11. }  
  12. static void printInterfaceNames(Object o) {  
  13. Class c = o.getClass();  
  14. Class[] theInterfaces = c.getInterfaces();  
  15. for (int i = 0; i < theInterfaces.length; i++) {  
  16. String interfaceName = theInterfaces[i].getName();  
  17. System.out.println(interfaceName);  
  18. }  
  19. }  

6.Examining Interfaces

判定一个类是不是接口

 
 
  1. import java.lang.reflect.*;  
  2. import java.util.*;  
  3. class SampleCheckInterface {  
  4. public static void main(String[] args) {  
  5. Class thread = Thread.class;  
  6. Class runnable = Runnable.class;  
  7. verifyInterface(thread);  
  8. verifyInterface(runnable);  
  9. }  
  10. static void verifyInterface(Class c) {  
  11. String name = c.getName();  
  12. if (c.isInterface()) {  
  13. System.out.println(name + " is an interface.");  
  14. } else {  
  15. System.out.println(name + " is a class.");  
  16. }  
  17. }  

如:c.isInterface()

7.Identifying Class Fields

找出指定类所有的域成员

每个数据成员可以用java.reflect.Field来封闭其名称,类型,修改符的集合。也可以通过相应的方法获取或设置到该成员的值。

如:

 
 
  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleField {  
  4. public static void main(String[] args) {  
  5. GridBagConstraints g = new GridBagConstraints();  
  6. printFieldNames(g);  
  7. }  
  8. static void printFieldNames(Object o) {  
  9. Class c = o.getClass();  
  10. Field[] publicFields = c.getFields();  
  11. for (int i = 0; i < publicFields.length; i++) {  
  12. String fieldName = publicFields[i].getName();  
  13. Class typeClass = publicFields[i].getType();  
  14. String fieldType = typeClass.getName();  
  15. System.out.println("Name: " + fieldName +   
  16. ", Type: " + fieldType);  
  17. }  
  18. }  

8.Discovering Class Constructors

检索指定类的构造函数

当创建一个类的实例时,是通过检造方法来作的,这种方法可以被重载。

每一个构造方法可以用类Constructor来描述,,包括名称,修饰符,参数类型(Class[]),和异常列表。

可以通过一个Class的getConstructors方法获取到该类的Constructor数组。

例程:

 
 
  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleConstructor {  
  4. public static void main(String[] args) {  
  5. Rectangle r = new Rectangle();  
  6. showConstructors(r);  
  7. }  
  8. static void showConstructors(Object o) {  
  9. Class c = o.getClass();  
  10. Constructor[] theConstructors = c.getConstructors();  
  11. for (int i = 0; i < theConstructors.length; i++) {  
  12. System.out.print("( ");  
  13. Class[] parameterTypes =   
  14. theConstructors[i].getParameterTypes();  
  15. for (int k = 0; k < parameterTypes.length; k ++) {  
  16. String parameterString = parameterTypes[k].getName();  
  17. System.out.print(parameterString + " ");  
  18. }  
  19. System.out.println(")");  
  20. }  
  21. }  

9.Obtaining Method Information

检索方法

可以找到隶属于一个类的所有方法,通过getMethods包含Method数组,进而得到该方法的返回类型,修饰符,方法名称,参数列表

步骤:

a.指定类的Class Object

b.getMethods()获取Method[]对象

c,遍历该数组对象

例程:

 
 
  1. import java.lang.reflect.*;  
  2. import java.awt.*;  
  3. class SampleMethod {  
  4. public static void main(String[] args) {  
  5. Polygon p = new Polygon();  
  6. showMethods(p);  
  7. }  
  8. static void showMethods(Object o) {  
  9. Class c = o.getClass();  
  10. Method[] theMethods = c.getMethods();  
  11. for (int i = 0; i < theMethods.length; i++) {  
  12. String methodString = theMethods[i].getName();  
  13. System.out.println("Name: " + methodString);  
  14. String returnString =  
  15. theMethods[i].getReturnType().getName();  
  16. System.out.println(" Return Type: " + returnString);  
  17. Class[] parameterTypes = theMethods[i].getParameterTypes();  
  18. System.out.print(" Parameter Types:");  
  19. for (int k = 0; k < parameterTypes.length; k ++) {  
  20. String parameterString = parameterTypes[k].getName();  
  21. System.out.print(" " + parameterString);  
  22. }  
  23. System.out.println();  
  24. }  
  25. }  

希望通过以上内容的介绍,能够给你带来帮助。

网站题目:详细介绍java的反射技术
本文地址:http://www.shufengxianlan.com/qtweb/news9/168259.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联