实体类
package pojo; public class User { private int id; private String name; private String pwd; //构造,有参,无参 //set/get //toString() @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + ''' + ", pwd='" + pwd + ''' + '}'; } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public User() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }获得类的属性和指定属性的值
Class c1 = Class.forName("pojo.User"); System.out.println(c1.getName());//包名+类名 pojo.User System.out.println(c1.getSimpleName());//类名 User //获得类的属性 Field[] fields = c1.getFields();//只能找到public属性 for (Field field : fields) { System.out.println(field); } fields=c1.getDeclaredFields();//获得全部的属性 for (Field field : fields) { System.out.println(field); } //private int pojo.User.id //private java.lang.String pojo.User.name //private java.lang.String pojo.User.pwd System.out.println("========================="); //获取指定属性的值 Field name = c1.getDeclaredField("name"); System.out.println(name); //private java.lang.String pojo.User.name获取类的方法和指定方法
Class c1 = Class.forName("pojo.User"); //获取类的方法 Method[] methods = c1.getMethods();//获得本类及其父类的全部public方法 for (Method method : methods) { System.out.println(method); } //public java.lang.String pojo.User.toString() //public java.lang.String pojo.User.getName() //public int pojo.User.getId() //public void pojo.User.setName(java.lang.String) //public java.lang.String pojo.User.getPwd() //public void pojo.User.setPwd(java.lang.String) //public void pojo.User.setId(int) //public final void java.lang.Object.wait() throws java.lang.InterruptedException //public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException //public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException //public boolean java.lang.Object.equals(java.lang.Object) //public native int java.lang.Object.hashCode() //public final native java.lang.Class java.lang.Object.getClass() //public final native void java.lang.Object.notify() //public final native void java.lang.Object.notifyAll() System.out.println("===================="); methods=c1.getDeclaredMethods();//获取本类的所有方法 for (Method method : methods) { System.out.println(method); } //public java.lang.String pojo.User.toString() //public java.lang.String pojo.User.getName() //public int pojo.User.getId() //public void pojo.User.setName(java.lang.String) //public java.lang.String pojo.User.getPwd() //public void pojo.User.setPwd(java.lang.String) //public void pojo.User.setId(int) System.out.println("=============="); //获得指定方法 Method getName = c1.getMethod("getName", null); Method setName = c1.getMethod("setName", String.class); System.out.println(getName);//public java.lang.String pojo.User.getName() System.out.println(setName);//public void pojo.User.setName(java.lang.String)获得构造器和指定构造器
Class c1 = Class.forName("pojo.User"); Constructor[] constructors = c1.getConstructors();//获得public构造器 for (Constructor constructor : constructors) { System.out.println(constructor); } //public pojo.User() //public pojo.User(int,java.lang.String,java.lang.String) System.out.println("==========="); constructors=c1.getDeclaredConstructors();//获得全部构造器 for (Constructor constructor : constructors) { System.out.println(constructor); } //public pojo.User() //public pojo.User(int,java.lang.String,java.lang.String) //获得指定构造器 Constructor constructor = c1.getConstructor(int.class, String.class, String.class); System.out.println(constructor); //public pojo.User(int,java.lang.String,java.lang.String) }动态创建对象执行方法 构造一个对象、通过构造器创建对象、通过反射调用普通方法、通过反射 *** 作属性
Class c1 = Class.forName("pojo.User"); //构造一个对象 User user = (User) c1.newInstance();//调用的无参构造器 System.out.println(user);//User{id=0, name='null', pwd='null'} //通过构造器创建对象 Constructor constructor = c1.getConstructor(int.class, String.class, String.class); User user1 = (User) constructor.newInstance(1, "zb", "123"); System.out.println(user1);//User{id=1, name='zb', pwd='123'} //通过反射调用普通方法 User user2 = (User) c1.newInstance(); Method setName = c1.getDeclaredMethod("setName", String.class); setName.invoke(user2,"zb"); System.out.println(user2.getName());//zb //通过反射 *** 作属性 User user3 = (User) c1.newInstance(); Field name = c1.getDeclaredField("name"); //不能直接 *** 作private的成员,需要关闭安全监测(方法、属性和构造器都有setAccessible方法) name.setAccessible(true); name.set(user3,"zbzb"); System.out.println(user3.getName());//zbzb }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)