成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

詳細介紹java的反射技術

開發 后端
本文介紹的是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. }  

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

責任編輯:于鐵 來源: 互聯網
相關推薦

2011-07-12 10:24:17

類加載反射

2009-06-29 14:30:27

JSF技術

2010-03-16 14:46:37

2011-07-22 16:37:01

java接口

2011-07-11 16:55:31

Java

2010-03-18 17:39:30

低耗能無線技術

2009-12-23 11:09:57

軟交換技術

2021-11-26 07:31:43

Java反射程序

2010-09-30 13:58:28

2010-09-09 14:25:32

2010-03-18 14:27:53

Java Thread

2009-06-11 10:00:05

Java Socket

2011-07-11 11:02:12

JAVA集合框架

2011-07-22 17:41:02

java

2009-12-31 16:38:56

VPN光纖接入技術

2010-03-18 18:20:34

Java Socket

2011-07-11 15:02:54

枚舉

2011-07-11 17:33:25

JAVA可移植性

2011-07-21 13:51:38

java

2011-07-21 14:15:08

java
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 男人av在线| 婷婷中文在线 | 成人免费影院 | 91新视频| 国产精品一区二区视频 | 欧美久久一级特黄毛片 | 欧美极品视频 | 日韩欧美三级电影 | 精品久久久久久久久久久 | 精品在线一区二区 | 欧美在线观看黄色 | 欧美成人一区二区三区片免费 | 亚洲欧美视频一区 | 福利国产 | 新疆少妇videos高潮 | 91在线| 国产高清免费 | 久久久久国产精品 | 日日操视频| www.亚洲国产精品 | 在线观看视频中文字幕 | 黄色永久免费 | 国产在线视频网 | 亚洲一区二区三区四区五区中文 | 国产一区二区精品在线观看 | 欧美精品一区二区三区蜜桃视频 | 精品视频999 | 妖精视频一区二区三区 | 亚洲欧美视频一区 | 精品久久久久一区 | a欧美| 91精品国产91综合久久蜜臀 | 亚洲人在线观看视频 | 成人乱人乱一区二区三区软件 | 四季久久免费一区二区三区四区 | 欧美全黄 | 久久高清免费视频 | 日本不卡高清视频 | 久久久久久久久99 | 欧美成人激情视频 | 中文字幕高清一区 |