以下几个类就是目标,就是想搞一下几个类中的变量和方法。(代码是模仿别人写的http://www.blogjava.net/JFire/archive/2008/11/17/240958.html)
package com.agei.test;
/**
* 定义照相机接口
* @author Agei
*
*/
public interface Camera {
public void takePhoto();
}
package com.agei.test;
public class Camera01 implements Camera{
private final int prefixs = 300;
private final double optionZoom = 3.5;
@Override
public void takePhoto() {
// TODO Auto-generated method stub
System.out.println("Camera01:prefixs=" + prefixs + " optionZoom=" + optionZoom);
}
}
package com.agei.test;
public class Camera02 implements Camera{
private final int prefixs = 300;
private final double optionZoom = 5;
@Override
public void takePhoto() {
System.out.println("Camera02: prefixs = " + prefixs + " optionZoom = " + optionZoom);
}
}
以上三个类就是模拟的目标类,在现实中这个就是封装好的jar包,现在开始正式盗窃里面的内容:
package com.agei.test;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Seller类利用java的反射机制实现
* @author Vgei
*
*/
public class Seller {
/**
* 向顾客描述商品信息
* @param type 类名(eg:com.agei.test.Camera01)
*/
public void getDescription(String type){
try {
Class<?> cla = Class.forName(type);
//生成实例对象,在编译时我们并不知道obj是什么类型
Object obj = cla.newInstance();
//获得type类所定义的类变量及其方法
//获取类中所定义的变量
Field[] fields = cla.getDeclaredFields();
//获取类中所定义的方法
Method[] methods = cla.getDeclaredMethods();
System.out.print("the arguments of this Camera is:");
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
//输入类变量的定义及obj实例中对应的值
System.out.println(fields[i] + ":" + fields[i].get(obj));
}
System.out.print("the function of this Camera is:");
for (int i = 0; i < methods.length; i++) {
methods[i].setAccessible(true);
//输出类中方法的定义
System.out.println(methods[i]);
}
System.out.println("===========end=========");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使用商品的某个功能
* @param obj 所要调用类的对象
* @param function 调用想要调用类中的方法
*/
public void testFunction(Object obj,String function){
try {
Class<?> cla = obj.getClass();
//获取cla类中定义的无参方法
Method m = cla.getMethod(function,null);
//调用obj中名为function的无参方法
m.invoke(obj, null);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 拿商品给顾客
* @param type
* @return Object 类返回的对象
*/
public Object getCamera(String type){
try {
Class<?> cla = Class.forName(type);
Object obj = cla.newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
以上类中的方法就是利用类名来获取里面的属性和方法:
下边就是测试类,见证一下成果,将想要的东西全部输出
package com.agei.test;
public class Customer {
public static void main(String[] args) {
//找到一个售货员
Seller seller = new Seller();
//向售货员询问两种相机的信息
seller.getDescription("com.agei.test.Camera01");
seller.getDescription("com.agei.test.Camera02");
//让售货员那里Camera02
Camera camera = (Camera) seller.getCamera("com.agei.test.Camera02");
//试一下拍照功能
seller.testFunction(camera, "takePhoto");
}
}
输出结果:
the arguments of this Camera is:private final int com.agei.test.Camera01.prefixs:300
private final double com.agei.test.Camera01.optionZoom:3.5
the function of this Camera is:public void com.agei.test.Camera01.takePhoto()
===========end=========
the arguments of this Camera is:private final int com.agei.test.Camera02.prefixs:300
private final double com.agei.test.Camera02.optionZoom:5.0
the function of this Camera is:public void com.agei.test.Camera02.takePhoto()
===========end=========
Camera02: prefixs = 300 optionZoom = 5.0
大功告成!将Camera01,Camera02中的内容顺利剽窃!呵呵!有点邪恶哈!以上就是个人所理解的java反射机制,上网上看看了说框架利用反射的东西比较多,大家可以研究研究,就是jdbc连接数据库中的class.forname("ddd");这个肯定是用的是反射,呵呵!个人粗浅的理解!