自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳細(xì)介紹java的反射技術(shù)

開發(fā) 后端
本文介紹的是java的反射技術(shù),希望對(duì)你有幫助,一起來看。

反射的定義:審查元數(shù)據(jù)并收集關(guān)于它的類型信息的能力。下面介紹java的反射技術(shù)。

Lesson: 檢測(cè)類examing class

1.Retrieving Class Objects

獲取一個(gè)Class對(duì)象(metadata)

a,從對(duì)象的實(shí)例獲取。

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

b,從子類的實(shí)例獲取

  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,如果類名在編譯時(shí)是未知的,則可以使用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()方法獲取一個(gè)整型標(biāo)識(shí)值。

b.通過java.reflect.Modifier對(duì)象的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

檢索指定類實(shí)現(xiàn)的接口

例如:

  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

判定一個(gè)類是不是接口

  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

找出指定類所有的域成員

每個(gè)數(shù)據(jù)成員可以用java.reflect.Field來封閉其名稱,類型,修改符的集合。也可以通過相應(yīng)的方法獲取或設(shè)置到該成員的值。

如:

  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

檢索指定類的構(gòu)造函數(shù)

當(dāng)創(chuàng)建一個(gè)類的實(shí)例時(shí),是通過檢造方法來作的,這種方法可以被重載。

每一個(gè)構(gòu)造方法可以用類Constructor來描述,,包括名稱,修飾符,參數(shù)類型(Class[]),和異常列表。

可以通過一個(gè)Class的getConstructors方法獲取到該類的Constructor數(shù)組。

例程:

  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

檢索方法

可以找到隸屬于一個(gè)類的所有方法,通過getMethods包含Method數(shù)組,進(jìn)而得到該方法的返回類型,修飾符,方法名稱,參數(shù)列表

步驟:

a.指定類的Class Object

b.getMethods()獲取Method[]對(duì)象

c,遍歷該數(shù)組對(duì)象

例程:

  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. }  

希望通過以上內(nèi)容的介紹,能夠給你帶來幫助。

責(zé)任編輯:于鐵 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-12 10:24:17

類加載反射

2009-06-29 14:30:27

JSF技術(shù)

2010-03-16 14:46:37

2011-07-11 16:55:31

Java

2011-07-22 16:37:01

java接口

2021-11-26 07:31:43

Java反射程序

2010-03-18 17:39:30

低耗能無線技術(shù)

2009-12-23 11:09:57

軟交換技術(shù)

2011-07-11 11:02:12

JAVA集合框架

2009-06-11 10:00:05

Java Socket

2011-07-22 17:41:02

java

2010-03-18 14:27:53

Java Thread

2011-07-21 13:51:38

java

2011-07-21 14:15:08

java

2010-03-18 18:20:34

Java Socket

2011-07-11 15:02:54

枚舉

2011-07-11 17:33:25

JAVA可移植性

2011-07-21 15:44:33

Java內(nèi)部類

2010-09-30 13:58:28

2010-09-09 14:25:32

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)