标签:style blog io ar color os 使用 sp java
反射机制是java中非常重要的功能,熟练使用反射功能对实际的开发有很大的帮助。
一,通过反射获取对象实例
使用的对象User
1 package whroid.java.reflect; 2 3 public class User { 4 5 public String id; 6 private String name; 7 protected int age; 8 public String getId() { 9 return id; 10 } 11 public void setId(String id) { 12 this.id = id; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public int getAge() { 21 return age; 22 } 23 public void setAge(int age) { 24 this.age = age; 25 } 26 @Override 27 public String toString() { 28 return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; 29 } 30 31 }
通过class.forName() 获取对象,括号中是对象的全称
1 public static void main(String[] args) { 2 try { 3 Class cls = Class.forName("whroid.java.reflect.User"); 4 User user = (User) cls.newInstance(); 5 user.setAge(2); 6 user.setId("id is ok "); 7 user.setName("hi"); 8 System.out.println(user); 9 } catch (ClassNotFoundException | InstantiationException 10 | IllegalAccessException e) { 11 e.printStackTrace(); 12 } 13 }
运行后输出:
User [id=id is ok , name=hi, age=2]
二,通过反射获取对象的属性,以及方法等
使用获取对象的属性
1 public static void main(String[] args) { 2 Class<?> cls; 3 try { 4 cls = Class.forName("whroid.java.reflect.User"); 5 Field[] fileds = cls.getDeclaredFields(); 6 for (Field filed : fileds) { 7 System.out.println(Modifier.toString(filed.getModifiers()) 8 + " " + filed.getType() + " " + filed.getName()); 9 } 10 11 } catch (ClassNotFoundException e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } 15 }
运行后输出:
public class java.lang.String id
private class java.lang.String name
protected int age
获取对象方法
1 public static void main(String[] args) { 2 Class<?> cls; 3 try { 4 cls = Class.forName("whroid.java.reflect.User"); 5 Method[] methods = cls.getDeclaredMethods(); 6 for (Method method : methods) { 7 System.out.println(Modifier.toString(method.getModifiers())+" "+method.getReturnType()+" "+method.getName()+"("+method.getParameterTypes()+")"); 8 } 9 10 } catch (ClassNotFoundException e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace(); 13 } 14 }
运行后返回:
public class java.lang.String toString([Ljava.lang.Class;@4d12ee4f)
public class java.lang.String getName([Ljava.lang.Class;@15dbac11)
public void setName([Ljava.lang.Class;@8fd9b4d)
public class java.lang.String getId([Ljava.lang.Class;@41dee0d7)
public void setAge([Ljava.lang.Class;@f7b650a)
public void setId([Ljava.lang.Class;@6e267b76)
public int getAge([Ljava.lang.Class;@2073b879)
三,通过反射调用对象方法
1 public static void main(String[] args) { 2 Class<?> cls; 3 try { 4 cls = Class.forName("whroid.java.reflect.User"); 5 Method method = cls.getDeclaredMethod("setName", String.class); 6 User user = (User) cls.newInstance(); 7 method.invoke(user, "zhang shan‘ song"); 8 Method getNameMethod = cls.getDeclaredMethod("getName"); 9 String name = (String) getNameMethod.invoke(user); 10 System.out.println("name:" + name); 11 System.out.println("user:" + user); 12 13 } catch (ClassNotFoundException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 } catch (NoSuchMethodException e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } catch (SecurityException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } catch (IllegalAccessException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } catch (IllegalArgumentException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } catch (InvocationTargetException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } catch (InstantiationException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } 35 }
运行后返回:
name:zhang shan‘ song
user:User [id=null, name=zhang shan‘ song, age=0]
四,动态代理的使用
接口
1 package whroid.java.reflect; 2 3 public interface IPersion { 4 String getName(); 5 void setName(String name); 6 }
对象类
1 package whroid.java.reflect; 2 3 public class ChinesePeople implements IPerson{ 4 5 String name; 6 @Override 7 public String getName() { 8 // TODO Auto-generated method stub 9 return name; 10 } 11 @Override 12 public void setName(String name) { 13 // TODO Auto-generated method stub 14 this.name= name; 15 System.out.println("设置名称为:"+name); 16 } 17 18 @Override 19 public String toString() { 20 return "ChinesePeople [name=" + name + "]"; 21 } 22 }
使用InvocationHandler实现自己的代理类
1 package whroid.java.reflect; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5 import java.lang.reflect.Proxy; 6 7 //我的代理类 8 public class TestInvocationHandler implements InvocationHandler { 9 Object obj;//代理的对象 10 public TestInvocationHandler(Object obj) { 11 this.obj = obj; 12 } 13 14 @Override 15 public Object invoke(Object proxy, Method method, Object[] args) 16 throws Throwable { 17 //在方法实际方法运行前,我的代理类进行运行前的相应操作 18 System.out.println("before run:"+method.getName()); 19 //实际运行的方法 20 Object result = method.invoke(obj, args); 21 //在方法实际运行后,我的代理类可以进行一些收尾工作 22 System.out.println("after run:"+method.getName()); 23 return result; 24 } 25 26 public static void main(String[] args) { 27 ChinesePeople people = new ChinesePeople(); 28 //参数传入代理对象的类加载器和其对应的接口,以及传入代理对象的代理类 29 IPerson pe = (IPerson) Proxy.newProxyInstance(people.getClass() 30 .getClassLoader(), people.getClass().getInterfaces(), 31 new TestInvocationHandler(people)); 32 pe.setName("hello"); 33 } 34 }
运行后返回:
before run:setName
设置名称为:hello
after run:setName
标签:style blog io ar color os 使用 sp java
原文地址:http://www.cnblogs.com/whroid/p/4122220.html