package com.bjsxt.test.bean; public class User { private int id; private int age; private String uname; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public void setUname() { this.uname = "高淇"; } public User(int id, int age, String uname) { super(); this.id = id; this.age = age; this.uname = uname; } //javabean必须要有无参的构造方法! public User() { } }
package com.bjsxt.test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * 应用反射的API,获取类的信息(类的名字、属性、方法、构造器等) * @author 尚学堂高淇 www.sxt.cn * */ public class Demo02 { public static void main(String[] args) { String path = "com.bjsxt.test.bean.User"; try { Class clazz = Class.forName(path); //获取类的名字 System.out.println(clazz.getName());//获得包名+类名:com.bjsxt.test.bean.User System.out.println(clazz.getSimpleName()); //获的类名:User //获取属性信息 // Field[] fields = clazz.getFields(); //只能获得public的field Field[] fields = clazz.getDeclaredFields();//获得所有的field Field f = clazz.getDeclaredField("uname"); System.out.println(fields.length); for(Field temp:fields){ System.out.println("属性:"+temp); } //获取方法信息 Method[] methods = clazz.getDeclaredMethods(); Method m01 = clazz.getDeclaredMethod("getUname", null); //如果方法有参,则必须传递参数类型对应的class对象 Method m02 = clazz.getDeclaredMethod("setUname", String.class); for(Method m:methods){ System.out.println("方法:"+m); } //获得构造器信息 Constructor[] constructors = clazz.getDeclaredConstructors(); Constructor c = clazz.getDeclaredConstructor(int.class,int.class,String.class); System.out.println("获得构造器:"+c); for(Constructor temp:constructors){ System.out.println("构造器:"+temp); } } catch (Exception e) { e.printStackTrace(); } } }
程序的运行结果是:
com.bjsxt.test.bean.User
User
3
属性:private int com.bjsxt.test.bean.User.id
属性:private int com.bjsxt.test.bean.User.age
属性:private java.lang.String com.bjsxt.test.bean.User.uname
方法:public int com.bjsxt.test.bean.User.getId()
方法:public java.lang.String com.bjsxt.test.bean.User.getUname()
方法:public void com.bjsxt.test.bean.User.setUname()
方法:public void com.bjsxt.test.bean.User.setUname(java.lang.String)
方法:public void com.bjsxt.test.bean.User.setId(int)
方法:public void com.bjsxt.test.bean.User.setAge(int)
方法:public int com.bjsxt.test.bean.User.getAge()
获得构造器:public com.bjsxt.test.bean.User(int,int,java.lang.String)
构造器:public com.bjsxt.test.bean.User()
构造器:public com.bjsxt.test.bean.User(int,int,java.lang.String)
package com.bjsxt.test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import com.bjsxt.test.bean.User; /** * 通过反射API动态的操作:构造器、方法、属性 * @author 尚学堂高淇 www.sxt.cn * */ public class Demo03 { public static void main(String[] args) { String path = "com.bjsxt.test.bean.User"; try { Class<User> clazz = (Class<User>) Class.forName(path); //通过反射API调用构造方法,构造对象 User u = clazz.newInstance(); //其实是调用了User的无参构造方法 System.out.println(u); Constructor<User> c = clazz.getDeclaredConstructor(int.class,int.class,String.class); User u2 = c.newInstance(1001,18,"高淇二"); System.out.println(u2.getUname()); //通过反射API调用普通方法 User u3 = clazz.newInstance(); Method method = clazz.getDeclaredMethod("setUname", String.class); method.invoke(u3, "高淇三"); //u3.setUname("高淇三"); System.out.println(u3.getUname()); //通过反射API操作属性 User u4 = clazz.newInstance(); Field f = clazz.getDeclaredField("uname"); f.setAccessible(true); //这个属性不需要做安全检查了,可以直接访问 f.set(u4, "高淇四"); //通过反射直接写属性 System.out.println(u4.getUname()); //通过反射直接读属性的值 System.out.println(f.get(u4)); } catch (Exception e) { e.printStackTrace(); } } }
程序的运行结果是:
com.bjsxt.test.bean.User@5cb08ba7
高淇二
高淇三
高淇四
高淇四
注意对应User类的private属性的成员变量和方法,外部进行反射需要赋值的时候需要f.setAccessible(true); 设置成true,表示允许外部进行访问