1,用是的TestBean
- package cn.itcast.day1;
-
/**
-
*
-
* @version 创建时间:2012-2-22 下午12:20:49
-
*
-
*/
-
-
public class TestBean
-
{
-
private int x;
-
private String str;
-
public int y;
-
-
-
public TestBean() {
-
super();
-
}
-
-
-
public TestBean(int x,int y) {
-
super();
-
this.x = x;
-
this.y = y;
-
}
-
public TestBean(int x, String str, int y) {
-
super();
-
this.x = x;
-
this.str = str;
-
this.y = y;
-
}
-
public int getX() {
-
return x;
-
}
-
public void setX(int x) {
-
this.x = x;
-
}
-
public String getStr() {
-
return str;
-
}
-
public void setStr(String str) {
-
this.str = str;
-
}
-
public int getY() {
-
return y;
-
}
-
public void setY(int y) {
-
this.y = y;
-
}
-
-
public int toSum() {
-
return (x+y);
-
}
-
@Override
-
public String toString() {
-
return "TestBean [x=" + x + ", str=" + str + ", y=" + y + "]";
-
}
-
-
}
2,构造方法的反射
- package cn.itcast.day1;
-
-
import java.lang.reflect.Constructor;
-
import java.lang.reflect.InvocationTargetException;
-
-
-
public class ConstrutctorReflect {
-
-
public static void main(String[] args) throws Exception{
-
// TODO Auto-generated method stub
-
Constructor constructor1 = Class.forName("cn.itcast.day1.TestBean").getConstructor(int.class,int.class);//想要哪个构造方法就传哪个类型
-
TestBean tb1=(TestBean) constructor1.newInstance(1,2);
-
-
Constructor constructor2 = Class.forName("cn.itcast.day1.TestBean").getConstructor(int.class,String.class,int.class);//想要哪个构造方法就传哪个类型
-
TestBean tb2=(TestBean) constructor2.newInstance(1,"abc",2);
-
-
System.out.println(tb1+"----"+tb2);
-
}
-
-
}
3,作用域反射
把TestBean 的set get方法全部屏蔽掉也可以用的,是无影响的;
对私有域的访问,要注意啦
- package cn.itcast.day1;
-
-
import java.lang.reflect.Field;
-
-
public class FieldsReflect
-
{
-
-
-
public static void main(String[] args) throws Exception
-
{
-
TestBean tb=new TestBean();
-
Field y=Class.forName("cn.itcast.day1.TestBean").getField("y");
-
y.set(tb,3);
-
System.out.println(y.get(tb));
-
Field x=Class.forName("cn.itcast.day1.TestBean").getField("x");
-
x.set(tb,1);
-
System.out.println(x.get(tb));
-
-
}
-
-
}
-
-
//输出
-
3
-
Exception in thread "main" java.lang.NoSuchFieldException: x
-
at java.lang.Class.getField(Unknown Source)
-
at cn.itcast.day1.FieldsReflect.main(FieldsReflect.java:15)
/*再来个一个,综合一点*/
- package cn.itcast.day1;
- import java.lang.reflect.Field;
- public class FieldsReflect
- {
- public static void main(String[] args) throws Exception
- {
- TestBean tb=new TestBean();
- Field y=Class.forName("cn.itcast.day1.TestBean").getField("y");
- y.set(tb,3);
- System.out.println(y.get(tb));
- Field x=Class.forName("cn.itcast.day1.TestBean").getDeclaredField("x");//取得声明过的变量,不管可见性
- x.setAccessible(true);//设为可见的
- x.set(tb,1);
- Field str=Class.forName("cn.itcast.day1.TestBean").getDeclaredField("str");
- str.setAccessible(true);
- str.set(tb, "abc");
- tb=(TestBean) func(tb);
- System.out.println(str.get(tb));
- }
-
- public static Object func(Object obj) throws Exception
- {
- Field [] fields=obj.getClass().getDeclaredFields();//获取所有声明属性
- for(Field field:fields)
- {
- if(field.getType()==java.lang.String.class)
- {
- field.setAccessible(true);
- String orgstr=(String)field.get(obj);
- //orgstr.replaceAll("a", "b");
- field.set(obj, orgstr.replaceAll("a", "b"));
- System.out.println(field.get(obj));
- break;
- }
- }
-
- return obj;
- }
-
- }
//输出
- 3
- bbc
- bbc
4,普通方法反射
- package cn.itcast.day1;
-
-
import java.lang.reflect.Method;
-
-
-
public class MethodsReflect {
-
-
public static void main(String[] args) throws Exception {
-
// TODO Auto-generated method stub
-
TestBean tb=new TestBean();
-
Method printMethod=Class.forName("cn.itcast.day1.TestBean").getMethod("print", int.class,String.class,int.class);
-
printMethod.invoke(tb,1,"vs",2);// 如果tb是null,则调用静态先可以为null,否则出错
-
}
-
-
}
5,一个综合的例子
阅读(576) | 评论(0) | 转发(0) |