当前位置:文档之家› Java反射机制(代码练习)

Java反射机制(代码练习)

//获得String类的所有方法 Method method[] = classType.getMethods(); // 所有公有的方法 //Method method[] = classType.getDeclaredMethods(); // 所有的方法, 包括受保护、私有的
//String 类的所有方法 for( int i = 0 ; i < method.length ; i++){
public class InvokeTester {
public int add(int param1 , int param2){ return param1 + param2 ;
} public String echo(String msg){
return "echo:" + msg ; }
public static void main(String[] args) throws Exception{
package com.cta.reflection.test;
import ng.reflect.Method;
public class DumpMethods {
public static void main(String[] args){
Class classType = String.class; // Class.forName("ng.String")
Class classType = object.getClass(); System.out.println("Class" + classType.getName()); //该类的完整类名
//根据该类的默认构造函数构建一个实例对象 Object objectCopy = classType.getConstructor(new Class[]{}).newInstance(new Object[]{});
customerCopy.getAge()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
} }
package com.cta.test;
import ng.reflect.Method;
/** * 通过 java 的反射机制动态修改对象的属性 st2(Customer o) {
try { Class c = o.getClass(); // getMethod 方法第一个参数指定一个需要调用的方法名称,第二个参数是需要
调用方法的参数类型列表,如无参数可以指定 null,该方法返回一个方法对象 @SuppressWarnings("unchecked")
public static void main(String[] args){
Customer customer = new Customer("Tom" , 21);
customer.setId(new Long(1));
try { Customer customerCopy = (Customer)new ReflectTester().copy(customer); System.out.println("Copy information:" + customerCopy.getName() + " " +
Method sAge = c.getMethod("setAge", new Class[] { int.class }); Method gAge = c.getMethod("getAge", null); Method sName = c.getMethod("setName", new Class[] { String.class }); // 动态修改 Customer 对象的 age Object[] args1 = { new Integer(25) }; sAge.invoke(o, args1); // 动态取得 Customer 对象的 age Integer AGE = (Integer) gAge.invoke(o, null); System.out.println("the Customer age is: " + AGE.intValue()); // 动态修改 Customer 对象的 name Object[] args2 = { new String("李四") }; sName.invoke(o, args2);
System.out.println(method[i].toString()); }
} }
package com.cta.reflection.test;
public class Customer { private long id; private String name; private int age;
} package com.cta.reflection.test;
import ng.reflect.Field; import ng.reflect.Method;
public class ReflectTester {
public Object copy(Object object) throws Exception{
Object addresult = addMethod.invoke(invokeTester, new Object[]{new Integer(100),new Integer(39)});
System.out.println("add方法被调用了:" + addresult);
//调用echo方法 Method echoMethod = classType.getMethod("echo", new Class[]{String.class});
public Customer() { } public Customer(String name, int age) {
= name; this.age = age; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }
//获取和属性对应的 getXX()和 setXX()方法 Method getMethod = classType.getMethod(getMethodName, new Class[]{}); Method setMethod = classType.getMethod(setMethodName, new Class[]{field.getType()});
// 取 属 性 名 的 第 一 个
//获取和属性对应的 getXX()和 setXX()方法名 String getMethodName = "get" + fieldLetter + fieldName.substring(1); String setMethodName = "set" + fieldLetter + fieldName.substring(1);
//获得对象的所有属性 Field fields[] = classType.getDeclaredFields(); for (int i = 0 ; i < fields.length ; i++){
Field field = fields[i]; //System.out.println("属性的声明:" + field); String fieldName = field.getName(); //System.out.println("属性名是:" + fieldName); String fieldLetter = fieldName.substring(0, 1).toUpperCase(); 字母,首字母大写
Object echoresult = echoMethod.invoke(invokeTester, new Object[]{"abc"});
System.out.println("echo方法被调用了:" + echoresult);
} } /**
* /view/06db16bfc77da26925c5b018.html?from=related& hasrec=1
/** ==================================== 迷 糊 了 , 不 怎 么 懂 呢 ================================*/
//调用原对象的 getXXX()方法 Object value = getMethod.invoke(object, new Object[]{}); System.out.println(fieldName + " : " + value); //调用复制对象的 setXXX()方法 setMethod.invoke(objectCopy, new Object[]{value}); /** ================================================================================== ====*/ } return objectCopy; }
相关主题