反射是通过.class文件获取一个类的信息
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制
package edu.uestc.LearnningTest.Reflect; import lombok.Data; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @Data class Stu{
public String address ; private String name; private int age; public Stu(){
} public Stu(String name,int age){
this.name = name; this.age = age; } public static void main(String[] args) {
System.out.println("hello"); } } public class Learining1 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, NoSuchFieldException, InstantiationException {
/ * 第一种反射方法 obj.getClass() */ Stu stu1 = new Stu("zs",10); Class one =stu1.getClass(); System.out.println(one.getSimpleName()); / * 第二种反射方法obj.class */ Class two = Stu.class; System.out.println(one == two); / * 第三种反射方法obj.forname() 【推荐】 */ Class three = Class.forName("edu.uestc.LearnningTest.Reflect.Stu"); System.out.println(one == three); / * 获得所有的声明的字段包括共有,私有,保护字段 */ Field[] AllField ; AllField = three.getDeclaredFields(); for(Field s : AllField){
System.out.println(s.getName()); } / * 获得声明的公有变量 */ Field[] PrivateField; PrivateField = three.getFields(); for(Field s : PrivateField) System.out.println(s.getName()); / * 获得共有字段并调用 */ Field f = three.getField("address"); try{
//获得一个对象 //1 通过无参构造方法获取对象 Object ob = three.getConstructor().newInstance(); //在这个对象设置字段 f.set(ob,"10086"); //得到字段的值 Stu three1 = (Stu) ob; System.out.println(three1.getAddress()); / //2 通过有参构造方法获取对象 Object ob1 = three.getConstructor(String[].class,int[].class).newInstance("a","12345"); Stu three2 = (Stu) ob1; System.out.println(three2.getName()+three2.getAddress()+three2.getAge()); / } catch (InstantiationException e) {
e.printStackTrace(); } / * 获得私有变量并调用 */ Field f1 = three.getDeclaredField("name"); try{
//获得一个对象 Object obj = three.getConstructor().newInstance(); f1.setAccessible(true); //解除私有限制 f1.set(obj,"w5"); //验证 Stu three2 = (Stu) obj; System.out.println(three2.getName()); } catch (InstantiationException e) {
e.printStackTrace(); } / * 获取所有方法 */ Method[] m = three.getMethods(); for(Method m1 : m){
System.out.println(m1); } System.out.println(""); / * 获取私有变量方法 */ Method[] m1 = three.getDeclaredMethods(); for(Method m2 : m1){
System.out.println(m2); } / * 获取单个方法 */ Method m2 = three.getMethod("getAge",null); Method m3 = three.getDeclaredMethod("setName",String.class); //获得对象 try{
//实例化一个对象 Object obj = three.getConstructor().newInstance(); //实例化获得方法 m2.invoke(obj,null); m3.invoke(obj,"qiuqian"); Stu obj2 = (Stu)obj; System.out.println(obj2.getName()); } catch (InstantiationException e) {
e.printStackTrace(); } / * 反射main方法 */ Method mainMethod = three.getMethod("main",String[].class); Object obj3 = three.getConstructor().newInstance(); Stu obj4 = (Stu)obj3; mainMethod.invoke(obj4,(Object) new String[]{
"a","b","c"}); //mainMethod.invoke(null,(Object) new String[]{"a","b","c"}); } }
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/33398.html