标签:col ble block err 指定字段 declared ace turn getc
package sinldo; public class Person { String name; private int age; public Person() { System.out.println("无参构造器"); } public Person(String name,int age) { System.out.println("有参构造器"); this.name=name; this.age=age; } private void privaetMethod() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setName(String name, int age) { System.out.println("name: "+name); System.out.println("age: "+age); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
Class class1=null;//获得Class对象 //3种反射方式 //1.类.class class1=Person.class; //2.对象.getClass Person person=new Person();// class1=person.getClass(); //3.根据全类名,使用方法forName,此方法用的最多 try { class1=Class.forName("sinldo.Person");// } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }//全类名
Object per = class1.newInstance();//该方法获取的对象只能使用object的方法 Person per2=(Person) class1.newInstance();//这样就可以使用Person类的方法
//获取方法 Class<?> class2=Class.forName("sinldo.Person"); //1.得到对应的类中有哪些方法,不能获取私有方法,获取的普通方法,不能获取构造函数 Method[] methods = class2.getMethods(); for (Method method : methods) { System.out.println("方法;"+method); }
//2.获取所有方法,包括私有方法,只能获取本类声明的方法,不能获取继承的类 Method[] declaredMethods = class2.getDeclaredMethods(); for (Method method : declaredMethods) { System.out.println("getDeclaredMethods():"+method); }
//3.获取指定的方法,第一个是参数是方法名,后面的参数是方法里的参数 Method Method_setName = class2.getDeclaredMethod("setName", String.class); System.out.println("setName方法: "+Method_setName); Method Method_setName2=class2.getDeclaredMethod("setName", String.class,int.class); System.out.println(Method_setName2);
//执行方法 //要使用方法,先要创建一个对象,为执行方法做准备 Object obj = class2.newInstance();//获取一个对象 //第一个参数是对象名,后面的参数是方法的参数 Method_setName2.invoke(obj, "xuejie",23);
Field[] declaredFields = class2.getDeclaredFields(); for (Field field : declaredFields) { System.out.println("Person类的字段: "+field); }
//1.获取制定名字的Field Field personField_name = class2.getDeclaredField("name"); System.out.println("指定的字段: "+personField_name.getName());
//2.获取指定对象的Field的值,这个Field不能是私有的,不然获取不了 Person per3=new Person("xue", 12); Object object = personField_name.get(per3); System.out.println("获取指定对象的‘name’的Field的值: "+object);
//3.设置指定对象的Field的值
//第一个参数是对象名,第二个参数是给指定字段设置的值
personField_name.set(per3, "zhao"); System.out.println("设置指定对象字段name的Field的值为: "+per3.name);
//4.若字段是私有的,则需要调用setAccessible(true)方法
Field personField_age = class2.getDeclaredField("age"); personField_age.setAccessible(true); System.out.println("获取指定私有字段名: "+personField_age.getName()); Object object2 = personField_age.get(per3); System.out.println("获取指定对象字段age的Field的值: "+object2);
//获取构造器 //由Class对象的getConstructors()的方法获取所有的构造器 Constructor<Person>[] constructors = (Constructor<Person>[]) class2.getConstructors(); for (Constructor<Person> constructor : constructors) { System.out.println("Person类的构造器: "+constructor); }
//获取指定的构造器 Constructor<?> constructor = class2.getConstructor(String.class,int.class); System.out.println("指定的构造器: "+constructor);
Object newInstance = constructor.newInstance("xue",12);
System.out.println(newInstance);
标签:col ble block err 指定字段 declared ace turn getc
原文地址:http://www.cnblogs.com/xuesheng/p/7851655.html