java通过反射调用方法
2014-04-25 16:26
190 查看
java通过反射调用方法
java
反射
下面代码演示如何通过反射调用方法。
首先通过Class实例的getDeclaredMethods()获得所有方法的定义,然后通过Method的invoke方法触发方法调用,invoke方法的第一个参数是方法所属对象,第二个参数是方法调用的参数列表。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
*
* @author outofmemory.cn
*/
public class Main {
/**
* Invokes the methods of an object using the Reflection api.
*/
public void invokeMethodsUsingReflection() {
//Obtain the Class instance
Class computerClass = Computer.class;
//Get the methods
Method[] methods = computerClass.getDeclaredMethods();
//Create the object that we want to invoke the methods on
Computer computer = new Computer();
//Loop through the methods and invoke them
for (Method method : methods) {
Object result;
try {
//Call the method. Since none of them takes arguments we just
//pass an empty array as second parameter.
result = method.invoke(computer, new Object[0]);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
return;
} catch (InvocationTargetException ex) {
ex.printStackTrace();
return;
} catch (IllegalAccessException ex) {
ex.printStackTrace();
return;
}
System.out.println(method.getName() + ": " + result);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().invokeMethodsUsingReflection();
}
class Computer {
private String brand = "DELL";
private String type = "Laptop";
private int harddiskSize_GB = 300;
private boolean AntiVirusInstalled = true;
public String getBrand() {
return brand;
}
public String getType() {
return type;
}
public int getHarddiskSize_GB() {
return harddiskSize_GB;
}
public boolean isAntiVirusInstalled() {
return AntiVirusInstalled;
}
}
}
上面代码将输出:
getBrand: DELL
getHarddiskSize_GB: 300
isAntiVirusInstalled: true
getType: Laptop
java
反射
下面代码演示如何通过反射调用方法。
首先通过Class实例的getDeclaredMethods()获得所有方法的定义,然后通过Method的invoke方法触发方法调用,invoke方法的第一个参数是方法所属对象,第二个参数是方法调用的参数列表。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
*
* @author outofmemory.cn
*/
public class Main {
/**
* Invokes the methods of an object using the Reflection api.
*/
public void invokeMethodsUsingReflection() {
//Obtain the Class instance
Class computerClass = Computer.class;
//Get the methods
Method[] methods = computerClass.getDeclaredMethods();
//Create the object that we want to invoke the methods on
Computer computer = new Computer();
//Loop through the methods and invoke them
for (Method method : methods) {
Object result;
try {
//Call the method. Since none of them takes arguments we just
//pass an empty array as second parameter.
result = method.invoke(computer, new Object[0]);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
return;
} catch (InvocationTargetException ex) {
ex.printStackTrace();
return;
} catch (IllegalAccessException ex) {
ex.printStackTrace();
return;
}
System.out.println(method.getName() + ": " + result);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().invokeMethodsUsingReflection();
}
class Computer {
private String brand = "DELL";
private String type = "Laptop";
private int harddiskSize_GB = 300;
private boolean AntiVirusInstalled = true;
public String getBrand() {
return brand;
}
public String getType() {
return type;
}
public int getHarddiskSize_GB() {
return harddiskSize_GB;
}
public boolean isAntiVirusInstalled() {
return AntiVirusInstalled;
}
}
}
上面代码将输出:
getBrand: DELL
getHarddiskSize_GB: 300
isAntiVirusInstalled: true
getType: Laptop
相关文章推荐
- 通过Java反射调用方法
- Java通过反射实现方法调用
- java基础之反射-通过反射调用某个属性的setter和getter方法
- Java通过反射调用其他类的私有属性和私有方法
- 通过java反射调用远程方法
- 黑马程序员java高新技术篇----通过反射调用main方法
- 通过Java反射调用方法的实例,通过这个例子,能明白怎么用反射。
- 通过Java反射调用方法
- Java通过反射调用方法
- java 通过反射获取调用类方法及属性
- Java 动态的创建注入代码,注入方法给类或者接口并通过反射调用
- java 通过反射获取调用类方法及属性
- java通过反射调用类的private方法.
- 通过Java反射调用方法
- 通过Java反射调用方法
- 通过JAVA的反射调用类中的公有私有方法
- java通过反射获取调用变量以及方法
- java 通过反射获取调用类方法及属性
- 通过Java反射调用方法
- JAVA通过反射调用方法