反射机制

通过反射机制可以操作字节码文件(黑客哈哈哈)

package com.javastudy.example12;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.ResourceBundle;

public class ReflectTest01 {
    public static void main(String[] args) {
        Class c1 = null,c2=null;
        try {
             c1=Class.forName("java.lang.String"); //String类型
            c2=Class.forName("java.util.Date");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        String x="123";
        Class s1=x.getClass();//String类型
        System.out.println(s1 == c1);//true
        Date d=new Date();
        Class s2=d.getClass();
        System.out.println(s2==c2);//true
       //java中任意类型都有.class.属性
        Class z=String.class;
        System.out.println(z==s1);//true


        System.out.println("==============================");
        try {
            Class test=Class.forName("com.javastudy.example11.Test");
            System.out.println("newInstance之前");
            Object obj= test.newInstance();//实例化对象,调用无参构造
            System.out.println("newInstance之后");
            System.out.println(obj);//输出对象

            FileReader read=new FileReader("D:\\JavaStudy\\src\\com\\javastudy\\example12\\dd.properties");
            Properties pro=new Properties();
            pro.load(read);
          read.close();
          String className=pro.getProperty("className");
            System.out.println(className);
            System.out.println("==============资源绑定机制,方便================");
            ResourceBundle bundle=ResourceBundle.getBundle("com\\javastudy\\example12\\dd");//文件在src下,并且不加后缀!!!
            String className2=bundle.getString("className");
            System.out.println(className2);

            System.out.println("==============通过反射机制================");
            Class cla=Class.forName(className);

            Object obj2=cla.newInstance();
            System.out.println(obj2);//配置文件可修改

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

运行截图
image

例二

package com.javastudy.example12;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ReflectTest02 {
    public static void main(String[] args) {
        Class c1=null;
        try {
            c1=Class.forName("com.javastudy.example12.Test");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Field[] f=c1.getFields();
        System.out.println(f.length);          //输出公有属性个数
        Field[] f2=c1.getDeclaredFields();
        System.out.println(f2.length);        //输出属性个数
        System.out.println("=====================");
        for (Field fi:f2
             ) {
            //获取修饰符
            int count=fi.getModifiers();        //返回的数字的修饰符的代号
            String s= Modifier.toString(count);  //转换为字符串
            System.out.println(s);
            Class ftype=fi.getType();           //获取属性类型
            System.out.println(ftype.getName());
            System.out.println(fi.getName());  //获取属性的名字

            //获取并修改属性的值
            System.out.println("===================");
            try {
                Object obj=c1.newInstance();

                Field fx=c1.getDeclaredField("name");

                fx.set(obj,"2022");               //修改属性的值
                System.out.println(fx.get(obj));  //读取属性的值

                Field fx2=c1.getDeclaredField("no");
                fx2.setAccessible(true);//打破封装
                fx2.set(obj,2022);
                System.out.println("打破封装"+fx2.get(obj));
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

        }

    }
}

运行结果
image

posted @ 2022-09-03 15:53  零基础科班  阅读(25)  评论(0)    收藏  举报