package com.lovecoding.day23.relect02;

import org.junit.Test;

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

public class Test01 {

    @Test
    public void test04() throws Exception {
        //获取 class
        Class studentClass = Student.class;

        Class superclass = studentClass.getSuperclass();
        System.out.println(superclass);

        Class[] interfaces = studentClass.getInterfaces();

        for (Class anInterface : interfaces) {
            System.out.println(anInterface);
        }
    }

    @Test
    public void test03() throws Exception {
        //属性
        //获取 class
        Class studentClass = Student.class;

        Object o = studentClass.newInstance();

        Field[] declaredFields = studentClass.getDeclaredFields();

        for (Field declaredField : declaredFields) {

            System.out.println(declaredField);
        }

        Field name = studentClass.getDeclaredField("name");
        //设置可操作
        name.setAccessible(true);
        Object o1 = name.get(o);
        System.out.println(o1);

        name.set(o,"王五");
        Object o2 = name.get(o);
        System.out.println(o2);
    }

    @Test
    public void test02() throws Exception {
        //获取 class
        Class studentClass = Student.class;

        Object o = studentClass.newInstance();
        //方法
        Method[] declaredMethods = studentClass.getDeclaredMethods();

        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        //获取setName
        Method setName = studentClass.getDeclaredMethod("setName", String.class);

        //执行 setName
        Object ls = setName.invoke(o, "李四");
        System.out.println(ls);

        System.out.println(o);
    }

    @Test
    public void test01() throws Exception {
        //获取 class
        Class studentClass = Student.class;

        //构造方法
        Constructor[] declaredConstructors = studentClass.getDeclaredConstructors();

        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }

        //根据参数获取
        Constructor s1 = studentClass.getDeclaredConstructor(String.class, Integer.class);
        //创建
        Object zs = s1.newInstance("张三", 22);
        // = new Student("张三", 22)
        System.out.println(zs);

    }



}
