Java反射机制中,基本的API

【前言】我是一个典型的离代码很远的程序员

 

反射API:

 

package test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.TypeVariable; public class ReflectionTest { public static void main(String[] args) { try { System.out.println("***************************"); System.out.println("The class to be load is:"); //得到test2.TestClass的Class对象,注意这里需要加上 //TestClass的包名 Class testClass = Class.forName("test2.TestClass"); System.out.println(testClass); System.out.println("***************************"); System.out.println("The package TestClass belong to:"); //得到TestClass所在的package Package testPackage = testClass.getPackage(); System.out.println(testPackage); System.out.println("***************************"); System.out.println("Get the Modifiers of the TestClass"); int iModifier = testClass.getModifiers(); //需要使用Modifier.toString将iModifier转换成string String sModifier = Modifier.toString(iModifier); //判断是否该类是否是接口 boolean isModifier = Modifier.isInterface(iModifier); System.out.println(iModifier); System.out.println(sModifier); System.out.println(isModifier); System.out.println("***************************"); System.out.println("Get the name of class:"); String name = testClass.getName(); System.out.println(name); System.out.println("***************************"); System.out.println("Get type of TestClass"); //得到TestClass这个类的变量类型,像<T>之类的 TypeVariable[] name2 = testClass.getTypeParameters(); for(int i=0; i<name2.length; i++) { System.out.println(name2[i]); } System.out.println("***************************"); System.out.println("Get the super Class:"); Class supClass = testClass.getSuperclass(); System.out.println(supClass.getName()); System.out.println("***************************"); System.out.println("Get the interface immplemented by TestClass:"); Class[] interfaceClasses = testClass.getInterfaces(); for (int i = 0; i < interfaceClasses.length; i++) { System.out.println(interfaceClasses[i].getName()); } System.out.println("***************************"); System.out.println("Get the inner class of TestClass:"); Class interClass = testClass.getDeclaringClass(); //由于TestClass中没有内部类,所以interClass为null //System.out.println(interClass.getName()); System.out.println("***************************"); System.out.println("Get the constructor of TestClass:"); Constructor[] constructor = testClass.getDeclaredConstructors(); for (int i = 0; i < constructor.length; i++) { System.out.println(constructor[i]); } System.out.println("***************************"); System.out.println("Get the Fields of TestClass:"); //得到数据域 Field[] fields = testClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { System.out.println(fields[i]); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

测试用的TestClass:

package test2; import java.util.*; public class TestClass implements Comparable { private int id; private String name; public TestClass() { this.id = 1; this.name = "somebody"; } public TestClass(int id, String name) { this.id = id; this.name = name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public int getId(){ return this.id; } public int compareTo(Object obj) { return 0; } public String toString() { return "<"+ id + "> "+"<"+name+">"; } }

 

运行结果:

 

***************************
The class to be load is:
class test2.TestClass
***************************
The package TestClass belong to:
package test2
***************************
Get the Modifiers of the TestClass
1
public
false
***************************
Get the name of class:
test2.TestClass
***************************
Get type of TestClass
***************************
Get the super Class:
java.lang.Object
***************************
Get the interface immplemented by TestClass:
java.lang.Comparable
***************************
Get the inner class of TestClass:
***************************
Get the constructor of TestClass:
public test2.TestClass(int,java.lang.String)
public test2.TestClass()
***************************
Get the Fields of TestClass:
private int test2.TestClass.id
private java.lang.String test2.TestClass.name

 

 

你可能感兴趣的:(java,String,api,Class,interface,Constructor)