反射
概念:在运行期间,可以获得对象的类型,类型的构造方法,类型的方法,类型的属性等,让对象认识到自身的结构。
获取对象的类型:(有三种方法)
1.对象名.getCalss()
2.Class.forName("类命")
3.类命.classs
public class Demo1 { public static void main(String[] args) throws ClassNotFoundException { System.out.println(new Person().getClass()); System.out.println(Class.forName("org.westos.reflect.Person")); System.out.println(Person.class); } }
用反射的方式创建对象:
一般的创建对象方法:new 类命
用反射创建对象方法:对象的类型.newInstance()
public class Demo2 { public static void main(String[] args) throws IllegalAccessException, InstantiationException { Person person1 = new Person(); Person person2 = Person.class.newInstance(); } }
获取方法信息:
getMethods() 获取所有的公共方法,包括继承父类的。getDeclaredMethods() 获取本类的所有方法,包括被(private,protected,缺省,public)public class Demo3 { public static void main(String[] args) { Method[] methods = Person.class.getMethods(); for (Method method : methods) { System.out.println(method); } System.out.println("----------------------------------------------------------"); Method[] declaredMethods = Person.class.getDeclaredMethods(); for (Method declaredMethod : declaredMethods) { System.out.println(declaredMethod); } } }
getMethod() 获取所有的公共方法,包括继承父类的。getDeclaredMethods() 获取本类的所有方法,包括被(private,protected,缺省,public)
public class Demo4 { public static void main(String[] args) throws NoSuchMethodException { System.out.println(Person.class.getMethod("show")); System.out.println("-----------------------------------------------------------"); Method[] declaredMethods = Person.class.getDeclaredMethods(); for (Method declaredMethod : declaredMethods) { System.out.println(declaredMethod); } } }
获取属性的信息
getFields() 获取所有公共属性(包括继承的)
getDeclaredFields() 获取本类所有属性(private public protected 不加)(不包括继承的)
getField("属性名")
getDeclaredField("属性名")public class Demo5 { public static void main(String[] args) throws NoSuchFieldException { Field[] field = Person.class.getFields(); for (Field field1 : field) { System.out.println(field1); } System.out.println("----------------------------------------"); Field[] declaredFields = Person.class.getDeclaredFields(); for (Field declaredField : declaredFields) { System.out.println(declaredField); } System.out.println("-----------------------------------------"); Field name = Person.class.getField("name"); System.out.println(name); System.out.println("-----------------------------------------"); Field age = Person.class.getDeclaredField("age"); System.out.println(age); } }
获取构造方法:
getConstructors() 获取所有公共的构造方法
getDeclaredConstructors() 获取本类所有构造方法(private public protected 缺省)
getConstructor(int.class) 获取int参数类型的构造
getConstructor() 获得无参构造
反射调用方法:
正常调用: new 类名.方法名
反射调用:方法名.Invoke(new 类命) (利用反射可以调用私有方法,但必须把权限用setAccessiable()方法设为true)
ublic class Demo6 { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { System.out.println("正常调用"); Person person = new Person(); person.test1(); System.out.println("反射调用"); Method test1 = Person.class.getDeclaredMethod("test1"); test1.invoke(person); System.out.println("反射调用私有方法"); Method test2 = Person.class.getDeclaredMethod("test2"); test2.setAccessible(true); test2.invoke(person); } }
用放射调用类的私有构造方法来创建对象
public class Demo7 { public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Constructor<Person> constructor = Person.class.getDeclaredConstructor(); Person person = constructor.newInstance(); } }