2021-06-08

1 篇文章 0 订阅

Day15

反射

public interface Action {
    void run();
​
}



public interface Study {
    void study();
}
​
​
​
import java.lang.reflect.Field;
import java.util.Arrays;
​
public class Demo1 {
    public static void main(String[] args) throws Exception {
        String name = "com.briup.day15.Student";
        Class c = Class.forName(name);
        Field[] fields = c.getDeclaredFields();
        System.out.println(Arrays.toString(fields));
    }
}
​
​
​
public class Demo2 {
    public static void main(String[] args) throws Exception {
        Class c = int.class;
        System.out.println(c.getName());
        //类
        Class<Student> c1 =Student.class;
        Student student = new Student();
        Class<? extends Student> c6 = student.getClass();
        Class<?> c7 = Class.forName("com.briup.day15.Student");
        System.out.println(c7.getSimpleName());
        System.out.println(c1.getSimpleName());
        System.out.println(c6.getSimpleName());
        //数组
        int [] a = new int [4];
        Class c5 =int[].class;
        Class<? extends int[]> c2 = a.getClass();
        System.out.println(c2.getSimpleName());
        System.out.println(c5.getSimpleName());
        
        //接口
        Class<Test> c3= Test.class;
        Class<?> c4 = Class.forName("com.briup.day15.Test");
        System.out.println(c3.getName());
        System.out.println(c4.getName());
    }
}
interface Test{
    
}
​
​
​
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
​
public class Demo3 {
    public static void main(String[] args) throws Exception {
        Class c1 = Student.class;
        Student student = new Student();
        Class c2 = student.getClass();
        Class c3 = Class.forName("com.briup.day15.Student");
        
        //Class类型  c1操作对应的属性
        //Student访问name student.name
        Field field2 = c1.getDeclaredField("name");
        System.out.println(field2.getName());
        Field[] fields = c1.getDeclaredFields();//获取所有的属性
        for (Field field : fields) {
            System.out.println(Modifier.toString(field.getModifiers()));//获取修饰符
            System.out.println(field.getType());
            System.out.println(field.getName());
            System.out.println("----------------");
        }
    }
}
​
​
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
​
public class Demo4 {
    private static Class c1;
​
    public static void main(String[] args) throws Exception {   
        //看有哪些方法
        Class c1 = Class.forName("com.briup.day15.Student");
        Class[] classOfArray = {int.class,double.class};
        String methodName = "eat";
        Method m = c1.getMethod(methodName, classOfArray);
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            //方法中:修饰符 返回类型 方法名(参数) 异常
            System.out.println(Modifier.toString(method.getModifiers()));
            System.out.println(method.getReturnType());
            System.out.println(method.getName());
            System.out.println(Arrays.toString(method.getParameters()));
            System.out.println(Arrays.toString(method.getExceptionTypes()));
            System.out.println("-----------------");
        }
    }
}
​
​
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Arrays;
​
public class Demo5 {
    public static void main(String[] args) throws Exception {
        String className = "com.briup.day15.Student";
        Class c1 = Class.forName(className);
        //拿到构造器
        Constructor[] constructors = c1.getConstructors();
        Class[] paraArray = {String.class,int.class};
        c1.getConstructor(paraArray);
        for (Constructor constructor : constructors) {
            //修饰符   方法名   参数    异常
            System.out.println(Modifier.toString(constructor.getModifiers()));
            System.out.println(constructor.getName());
            System.out.println(Arrays.toString(constructor.getParameters()));
            System.out.println(Arrays.toString(constructor.getExceptionTypes()));
            System.out.println("-----------------");
        }
    }
}
​
​
import java.lang.reflect.Modifier;
import java.util.Arrays;
​
public class Demo6 {
    public static void main(String[] args) throws Exception {
        String className = "com.briup.day15.Student";
        Class c = Class.forName(className);
        System.out.println(c.getName());
        System.out.println(c.getSimpleName());
        System.out.println(Modifier.toString(c.getModifiers()));
        System.out.println(Arrays.toString(c.getInterfaces()));
        System.out.println(c.getSuperclass());
        System.out.println(c.getPackage());
    }
}
​
​
​
import java.lang.reflect.Field;
​
public class Demo7 {
    public static void main(String[] args) throws Exception {
        Student student = new Student();
        Class<? extends Student> c = student.getClass();
        Field f = c.getDeclaredField("age");
        //设置值
        //s.age = 15;
        f.set(student, 15);
        //System.out.println(s.age);
        System.out.println(f.get(student));
        
        System.out.println("-----------------");
        //private String name;
        Field f2 = c.getDeclaredField("name");
        f2.setAccessible(true);
        f2.set(student, "tom");
        System.out.println(f2.get(student));
        
        System.out.println("-----------------");
        //public static String gender;
        Field f3 = c.getField("gender");
        //student.gender = "男"; 
        f3.set(student, "男");
        //student.gender = "女"; 
        //f3.set(null, "女");
        System.out.println(f3.get(student));
        
    }
}
​
​
import java.lang.reflect.Method;
​
public class Demo8 {
    public static void main(String[] args) throws Exception{
        Student student = new Student();
        Class c = student.getClass();
        String methodName = "run";
        Class<?>[] parameters = {};//填参数
        Method method = c.getMethod(methodName, parameters);
        //Object[] arg = {1,1.0};//参数对应
        method.invoke(student,null);
        
        Student student1 = new Student();
        Class c1 = student1.getClass();
        String methodName1 = "eat";
        Class<?>[] parameters1 = {int.class,double.class};//填参数
        Method method1 = c1.getMethod(methodName1, parameters1);
        Object[] arg = {1,1.0};//参数对应
        method1.invoke(student,arg);
    }
}
​
​
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
​
import com.briup.day05.ninenine;
​
public class Demo9 {
    public static void main(String[] args) throws Exception{
        Class c = Student.class;
        //Student s = (Student) c.newInstance();
        //System.out.println(s);
        String className = "com.briup.day15.Student";
        String mothodName = "study";
        Class<?>[] parameterTypes = {String.class,int.class};
        Object[] initargs = {"tom",19};
        Class c1 = Class.forName(className);
        Constructor constructor= c1.getConstructor(parameterTypes); 
        Object object = constructor.newInstance(initargs);
        System.out.println(object);
        Demo9 d = new Demo9();
        d.creatObject(className,parameterTypes,initargs);
        d.test(className, mothodName);
​
    }
    //创建对象
    public Object creatObject(String className,Class<?>[] parameterTypes,Object[] initargs) throws Exception {
        Object result = null;
        Class c1 = Class.forName(className);
        Constructor constructor= c1.getConstructor(parameterTypes);         
        Object object = constructor.newInstance(initargs);
        System.out.println(object);
        return object;
    }
    
    public Object test(String className,String mothodName) throws Exception{
        Object result = null;
        Class c = Class.forName(className);
        Method method = c.getDeclaredMethod(mothodName, null);
        
        if (Modifier.isStatic(method.getModifiers())) {
            //类调用
            result = method.invoke(null, null);
        }else {
            //对象调用方法
            Object object = c.newInstance();
            result = method.invoke(object, null);
        }   
        return result;
    }
}
​
​
public class Student implements Action,Study{
    private String name;
    int age;
    public String gender;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Student() {
        // TODO Auto-generated constructor stub
    }
    public Student(String name,int age) {
        this.name=name;
        this.age = age;
    }
    public void eat(int age,double money) {
        System.out.println("吃蒸饺");
    }
    public void study() {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("run");
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "name: " + name + "   age: " + age ;
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值