反射的开头

反射的开头,第1张

反射的开头
package com;

import com.wgs.jdbc.Car;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test02 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        String classfullpath = "com.wgs.jdbc.Car";
        //获取到Car类  对应的Class对象
        //  表示不确定的Java类型
        Class cls = Class.forName(classfullpath);

        System.out.println(cls);//显示是类的Class对象。是Class对象,而不是本来那个类的对象。

        //得到包名:
        System.out.println(cls.getPackage().getName());

        //得到类名

        System.out.println(cls.getName());

        //通过对象来创建一个对象实例

        Object o = cls.newInstance();//这里面得到的就是一个Car对象了,可以进行转型了。
        Car o1 = (Car)cls.newInstance();
        //获取到那个对应的方法。
        Method myCar = cls.getMethod("myCar");
        Object invoke = myCar.invoke(o);

        //通过反射获取属性

        Field age = cls.getField("age");
        System.out.println("age" + age );//这样获得的就只是属性了,还并不是具体的值。

        System.out.println(age.get(o));//还是要具体的对象来得到这个属性的值。

        //通过反射来设置属性的值。
        age.set(o,119);//先设置好

        System.out.println(age.get(o));//然后在通过反射得到最后的值

        System.out.println("======");
        //希望得到所有的属性
        Field[] fields = cls.getFields();
        for (Field field :fields) {
            System.out.println(field.getName());
        }
    }
}

properties和反射:

package com;


import com.wgs.Cat;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        //传统的方法创建对象
//        Cat cat = new Cat();
//        cat.hi();

        //使用properties来加载配置文件
        //使用配置 文件中的信息创建一个类的过程。

        Properties properties = new Properties();//先创建properties对象
        properties.load(new FileInputStream("src\reflection\re.properties"));
        //先加载文件,使用字节输入流加载文件路径
        String classfullpath = properties.getProperty("classfullpath");
        String method = properties.getProperty("method");//通过路径加载文件的信息,返回的类型是String
        //System.out.println(classfullpath);//测试一下
        //System.out.println(method);

        //使用反射机制解决配置文件中创建对象的过程

        //1.先加载类,会返回class类型的对象,
        Class cls = Class.forName(classfullpath);

        //2.通过cls 得到你所加载的对象实例
        Object o = cls.newInstance();
//        Cat o = (Cat)cls.newInstance();//通过getClass得到运行类型
        //3.通过方法cls来得到你所加载的类中的方法对象,此时方法也被看成一个对象。
        Method method1 = cls.getMethod(method);
        //4.通过找到的方法来实现对   那个对象的逆调用  ,这里就相当于将一切反过来了,所以这就是反射
        //5.找到对象只是第一步,找到方法实现对象的逆调用,感觉这样才是反射

        method1.invoke(o);
        //反射机制是java的灵魂,为什么要用到反射呢,就是通过外部文件配置,在不修改原码的情况下,来实现程序的运行

        Field age = cls.getField("age");//首先拿到这个变量
        System.out.println(age.getClass());

        System.out.println(age.get(o));//反射就是反这来的,一定要指明是按个对象啊
        System.out.println("===========");

        Constructor constructor = cls.getConstructor();
        System.out.println(constructor);

        //针对后面的有参构造器

        Constructor constructor1 = cls.getConstructor(String.class);//据说传入的是类对象
        System.out.println(constructor1);

    }
}

反射的小练习:

        

package com;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class HomeWork {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
        //1.得到Class类
        Class privateTestClass = PrivateTest.class;
        PrivateTest privateTestobj = privateTestClass.newInstance();
        //得到Name属性
        Field name = privateTestClass.getDeclaredField("name");
        //私有属性需要爆破,
        name.setAccessible(true);
        System.out.println(name.get(privateTestobj));

        Method getName = privateTestClass.getMethod("getName");
        Object invoke = getName.invoke(privateTestobj);
        System.out.println("name = " + invoke);
        System.out.println("==================================后面是下一题");

        //1.Class类的forName方法得到File类的Class对象
        Class aClass = Class.forName("java.io.File");
        //得到所有的构造器:
        Constructor[] declaredConstructors = aClass.getDeclaredConstructors();
        //遍历输出,快捷键:declaredConstructor.for
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
        //单独得到一个构造器,传入指定的参数,来得到指定的构造器
        Constructor declaredConstructors1 = aClass.getDeclaredConstructor(String.class);
        String fileAllPath = "e:\new.txt";
//下面是通过构造器穿件一个对象。
        Object o = declaredConstructors1.newInstance(fileAllPath);//现在还还是创建了一个对象,还没有使用createNewFile,得到一个文件呢。
        //通过class类得到createNewFile方法。
        Method createNewFile = aClass.getMethod("createNewFile");//是一个无参的方法,后面就不用家伙是哪个*.class了。
        Object invoke1 = createNewFile.invoke(o);
        System.out.println(invoke1);
    }
}

class PrivateTest{
    private String name = "hello";

    public String getName(){
        return name;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4970926.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-13
下一篇2022-11-13

发表评论

登录后才能评论

评论列表(0条)

    保存