JAVA反射
程序员文章站
2024-01-21 14:32:10
...
动态调用方法
public class Ref {
/**
* @param message
*/
public Ref(String message){
System.out.println("Hello :" + message);
}
public void sayHello(String message){
System.out.println("Say Hello!" + message);
}
public static String show(){
return "static show()";
}
public static void main(String[] args)
throws SecurityException, NoSuchMethodException,
ClassNotFoundException, IllegalArgumentException,
InstantiationException, IllegalAccessException,
InvocationTargetException{
//得到Ref类c(元数据)
Class c = Class.forName("test.Ref");
//通过指定参数类型获取相应构造函数(对象)
Constructor constructor = c.getConstructor(String.class);
//通过构造函数实例化Ref类对象test
Ref test =(Ref)constructor.newInstance("Test Instructor");
//通过指定的方法名称取得对应方法对象
//*****************
//语法: 类名.getMethod("方法名称",形参类型列表)
//*****************
Method method = c.getMethod("sayHello", String.class);
//动态调用方法(非静态方法)
//*****************
//语法: method.invoke(对象实例, 实参列表);
//*****************
method.invoke(test, "Test method");
//动态调用方法(静态方法)
Method show = c.getMethod("show");
String str = (String)show.invoke(test); //静态方法调用方式1
//String str = (String)show.invoke(null);//静态方法调用方式2
System.out.println (str);
}
}
接口应用子对象
interface XY{
public void show();
}
class X implements XY{
public void show(){
System.out.println ("X Class");
}
}
class Y implements XY{
public void show(){
System.out.println ("Y Class");
}
}
public class Reflect{
static public void main(String[] args){
try{
Class c = Class.forName("Y");//X
XY t = (XY)c.newInstance(); //反射相应的一个子类对象
t.show(); //动态运行不同类的方法
X x = new X(); //X Class
Class xc = Class.forName("X");
System.out.println (xc.isInstance(x));//true
System.out.println (t instanceof XY); //true
System.out.println (t instanceof X); //true
System.out.println (t instanceof Y);//false
}catch(Exception e){}
}
}
反射类成员
import java.lang.reflect.*;
public class ReflectDemo{
public static void main(String[] args){
try{
/*得到该类的所有数据字段*/
String className = "Student";
Class c = Class.forName(className);
showFields(c); //显示该类所有属性信息
showConstructors(c); //显示所有构造方法
showMethods(c);//显示该类所有方法信息
}catch(Exception e){
e.printStackTrace();
}
}
/*得到该类的所有数据字段*/
public static void showFields(Class c){
Field fields[] = c.getDeclaredFields();
for(Field f : fields){
//1./*数据字段修饰符*/
String m =Modifier.toString(f.getModifiers());
//2./*字段数据类型名*/
Class type=f.getType(); String t =type.getName();
//3./*类的属性名称*/
String n = f.getName();
System.out.println (m+" "+t+" "+n);
}
}
/*得到构造器*/
public static void showConstructors(Class c){
Constructor[] cons=c.getDeclaredConstructors();
for(Constructor con : cons){
String m = Modifier.toString(con.getModifiers());
String n = con.getName();
System.out.print (m+" "+n+"(");
Class[] params = con.getParameterTypes();
for(int j=0;j<params.length; j++){
if(j==params.length-1){
System.out.print (params[j].getSimpleName());
}else
System.out.print (params[j].getSimpleName()+",");
}
System.out.println (")");
}
}
/*得到类里所有方法*/
public static void showMethods(Class c){
Method[] m=c.getMethods();
for(int i=0; i<m.length; i++){
/*方法修饰符*/
String modify = Modifier.toString(m[i].getModifiers());
System.out.print(modify + " ");
/*方法返回类型*/
Class returntype=m[i].getReturnType();
System.out.print(returntype.getName()+" ");
/*方法名称*/
String name = m[i].getName();
System.out.print(name + "(");
/*方法参数*/
Class[] params = m[i].getParameterTypes();
for(int j=0;j<params.length; j++){
if(j==params.length-1){
System.out.print (params[j].getSimpleName());
}else
System.out.print (params[j].getSimpleName()+",");
}
System.out.println (")");
}
}
}
student.java
public class Student{
public String name = "";
public int age = 0;
public Student(){}
public Student(String name, int age){
this.name = name;
this.age = age;
}
public void show(){
System.out.println ("反射测试."+this.name);
}
}
上一篇: Java反射详细介绍