Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2877899
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: Java

2012-02-22 12:36:07

1,用是的TestBean
  1. package cn.itcast.day1;
  2. /**
  3.  *
  4.  * @version 创建时间:2012-2-22 下午12:20:49
  5.  *
  6.  */

  7. public class TestBean
  8. {
  9.     private int x;
  10.     private String str;
  11.     public int y;
  12.     
  13.     
  14.     public TestBean() {
  15.         super();
  16.     }
  17.     

  18.     public TestBean(int x,int y) {
  19.         super();
  20.         this.x = x;
  21.         this.y = y;
  22.     }
  23.     public TestBean(int x, String str, int y) {
  24.         super();
  25.         this.x = x;
  26.         this.str = str;
  27.         this.y = y;
  28.     }
  29.     public int getX() {
  30.         return x;
  31.     }
  32.     public void setX(int x) {
  33.         this.x = x;
  34.     }
  35.     public String getStr() {
  36.         return str;
  37.     }
  38.     public void setStr(String str) {
  39.         this.str = str;
  40.     }
  41.     public int getY() {
  42.         return y;
  43.     }
  44.     public void setY(int y) {
  45.         this.y = y;
  46.     }

  47.     public int toSum() {
  48.         return (x+y);
  49.     }
  50.     @Override
  51.     public String toString() {
  52.         return "TestBean [x=" + x + ", str=" + str + ", y=" + y + "]";
  53.     }
  54.     
  55. }

2,构造方法的反射
  1. package cn.itcast.day1;

  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.InvocationTargetException;


  4. public class ConstrutctorReflect {

  5.     public static void main(String[] args) throws Exception{
  6.         // TODO Auto-generated method stub
  7.         Constructor constructor1 = Class.forName("cn.itcast.day1.TestBean").getConstructor(int.class,int.class);//想要哪个构造方法就传哪个类型
  8.         TestBean tb1=(TestBean) constructor1.newInstance(1,2);
  9.         
  10.         Constructor constructor2 = Class.forName("cn.itcast.day1.TestBean").getConstructor(int.class,String.class,int.class);//想要哪个构造方法就传哪个类型
  11.         TestBean tb2=(TestBean) constructor2.newInstance(1,"abc",2);
  12.         
  13.         System.out.println(tb1+"----"+tb2);
  14.     }

  15. }
3,作用域反射
  把TestBean 的set get方法全部屏蔽掉也可以用的,是无影响的;
  对私有域的访问,要注意啦
  
  1. package cn.itcast.day1;

  2. import java.lang.reflect.Field;

  3. public class FieldsReflect
  4. {


  5.     public static void main(String[] args) throws Exception
  6.     {
  7.         TestBean tb=new TestBean();
  8.         Field y=Class.forName("cn.itcast.day1.TestBean").getField("y");
  9.         y.set(tb,3);
  10.         System.out.println(y.get(tb));
  11.         Field x=Class.forName("cn.itcast.day1.TestBean").getField("x");
  12.         x.set(tb,1);
  13.         System.out.println(x.get(tb));
  14.         
  15.     }

  16. }

  17. //输出
  18. 3
  19. Exception in thread "main" java.lang.NoSuchFieldException: x
  20.     at java.lang.Class.getField(Unknown Source)
  21.     at cn.itcast.day1.FieldsReflect.main(FieldsReflect.java:15)

    /*再来个一个,综合一点*/
  22. package cn.itcast.day1;

  23. import java.lang.reflect.Field;

  24. public class FieldsReflect 
  25. {


  26. public static void main(String[] args) throws Exception 
  27. {
  28. TestBean tb=new TestBean(); 
  29. Field y=Class.forName("cn.itcast.day1.TestBean").getField("y");
  30. y.set(tb,3);
  31. System.out.println(y.get(tb));
  32. Field x=Class.forName("cn.itcast.day1.TestBean").getDeclaredField("x");//取得声明过的变量,不管可见性
  33. x.setAccessible(true);//设为可见的
  34. x.set(tb,1);
  35. Field str=Class.forName("cn.itcast.day1.TestBean").getDeclaredField("str");
  36. str.setAccessible(true);
  37. str.set(tb, "abc");
  38. tb=(TestBean) func(tb);
  39. System.out.println(str.get(tb));
  40. }
  41.     
  42. public static Object func(Object obj) throws Exception
  43. {
  44. Field [] fields=obj.getClass().getDeclaredFields();//获取所有声明属性
  45. for(Field field:fields)
  46. {
  47. if(field.getType()==java.lang.String.class)
  48. {
  49. field.setAccessible(true);
  50. String orgstr=(String)field.get(obj);
  51. //orgstr.replaceAll("a", "b");
  52. field.set(obj, orgstr.replaceAll("a", "b"));
  53. System.out.println(field.get(obj));
  54. break;
  55. }
  56. }
  57. return obj;
  58. }
  59. }

    //输出

  60. 3
  61. bbc
  62. bbc
  
4,普通方法反射
  1. package cn.itcast.day1;

  2. import java.lang.reflect.Method;


  3. public class MethodsReflect {

  4.     public static void main(String[] args) throws Exception {
  5.         // TODO Auto-generated method stub
  6.         TestBean tb=new TestBean();
  7.         Method printMethod=Class.forName("cn.itcast.day1.TestBean").getMethod("print", int.class,String.class,int.class);
  8.         printMethod.invoke(tb,1,"vs",2);// 如果tb是null,则调用静态先可以为null,否则出错
  9.     }

  10. }
5,一个综合的例子
阅读(582) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~