反射的学习

public class Example_01 {
    String s;
    int i,i2,i3;
    private Example_01(){}
    protected Example_01(String s,int i){
        this.s = s;
        this.i = i;
    }
    public Example_01(String... strings) throws  NumberFormatException{
        if(0 < strings.length){
            i = Integer.valueOf(strings[0]);
        }
        if(1 < strings.length){
            i2 = Integer.valueOf(strings[1]);
        }
        if(2 < strings.length){
            i3 = Integer.valueOf(strings[2]);
        }
    }
    public void print(){
        System.out.println("s="+s);
        System.out.println("i="+i);
        System.out.println("i2="+i2);
        System.out.println("i3="+i3);
    }
}

import java.lang.reflect.Constructor;

public class Main_01 {
    public static void main(String[] args) {
        Example_01 example = new Example_01("10","20","30");
        Class<? extends Example_01> exampleC = example.getClass();
        Constructor[] declaredConstructors = exampleC.getDeclaredConstructors();
        for(int i = 0; i < declaredConstructors.length; i++){
            Constructor<?> constructor = declaredConstructors[i];
            System.out.println("查看是否允许带有可变数量的参数:"+constructor.isVarArgs());
            System.out.println("该构造方法的入口参数类型依次为:");
            Class[] parameterTypes = constructor.getParameterTypes();
            for (int j = 0; j < parameterTypes.length; j++) {
                System.out.println(""+parameterTypes[j]);
            }
            System.out.println("该构造方法可能抛出的异常类型为:");
            Class[] exceptionTypes = constructor.getExceptionTypes();
            for (int j = 0; j < exceptionTypes.length ; j++) {
                System.out.println(""+exceptionTypes[j]);
            }
            Example_01 example2 = null;
            while (example2 == null){
                try{
                    if(i == 2){
                        example2 = (Example_01)constructor.newInstance();
                    }else if(i == 1){
                        example2 = (Example_01)constructor.newInstance("7",5);
                    }else {
                        Object[] parameters = new Object[]{new String[]{"100","200","300"}};
                        example2 = (Example_01)constructor.newInstance(parameters);
                    }
                }catch (Exception e){
                    System.out.println("在创建对象时抛出的异常,下面执行setAccessible()方法");
                    constructor.setAccessible(true);
                }
            }
            if(example2 != null){
                example2.print();
                System.out.println();
            }
        }
    }
}
public class Example_02 {
    int i;
    public float f;
    protected boolean b;
    private String s;
}
import java.lang.reflect.Field;

public class Main_02 {
    public static void main(String[] args) {
        Example_02 example = new Example_02();
        Class exampleC = example.getClass();
        Field[] declaredFields = exampleC.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field field = declaredFields[i];
            System.out.println("名称为:"+field.getName());
            Class fieldType = field.getType();
            System.out.println("类型为:"+field);
            boolean isTurn = true;
            while (isTurn){
                try {
                    isTurn = false;
                    System.out.println("修改前的值为:"+field.get(example));
                    if(fieldType.equals(int.class)){
                        System.out.println("利用方法setInt()修改成员变量的值");
                        field.setInt(example,168);
                    }else if(fieldType.equals(float.class)){
                        System.out.println("利用方法setFloat()修改成员变量的值");
                        field.setFloat(example,99.9F);
                    }else if(fieldType.equals(boolean.class)){
                        System.out.println("利用方法setBoolean()修改成员变量的值");
                        field.setBoolean(example,true);
                    }else {
                        System.out.println("利用方法set()方法修改成员变量的值");
                        field.set(example,"MWQ");
                    }
                    System.out.println("修改后的值为:"+field.get(example));
                }catch (Exception e){
                    System.out.println("在设置成员变量值时抛出的异常,下面执行setAccessible()方法");
                    field.setAccessible(true);
                    isTurn = true;
                }
            }
            System.out.println();
        }
    }
}
public class Example_03 {
    static void staticMethod(){
        System.out.println("执行staticMethod()方法");
    }
    public int publicMethod(int i){
        System.out.println("执行publicMethod()方法");
        return i*100;
    }
    protected int protectedMethod(String s,int i) throws NumberFormatException{
        System.out.println("执行protectedMethod()方法");
        return Integer.valueOf(s)+i;
    }
    private String privateMethod(String... strings){
        System.out.println("执行privateMethod()方法");
        StringBuilder  stringBuilder = new StringBuilder();
        for (int i = 0; i <strings.length ; i++) {
            stringBuilder.append(strings[i]);
        }
        return stringBuilder.toString();

    }
}
import java.lang.reflect.Method;

public class Main_03 {
    public static void main(String[] args) {
        Example_03 example = new Example_03();
        Class exampleC = example.getClass();
        Method[] declaredMethods = exampleC.getDeclaredMethods();
        for (int i = 0; i <declaredMethods.length ; i++) {
            Method method = declaredMethods[i];
            System.out.println("名称为:"+method.getName());
            System.out.println("是否允许带有可变数量的参数:"+method.isVarArgs());
            System.out.println("入口参数类型依次为:");
            Class[] parameterTypes = method.getParameterTypes();
            for (int j = 0; j < parameterTypes.length; j++) {
                System.out.println(parameterTypes[j]);
            }
            System.out.println("返回值类型为:"+method.getReturnType());
            System.out.println("可能抛出的异常类型有:");
            Class[] exceptionTypes = method.getExceptionTypes();
            for (int j = 0; j <exceptionTypes.length ; j++) {
                System.out.println(exceptionTypes[j]);
            }
            boolean isTurm = true;
            while (isTurm){
                try {
                    isTurm = false;
                    if("staticMethod".equals(method.getName())){
                        method.invoke(example);
                    }
                    else if("publicMethod".equals(method.getName())){
                        System.out.println("返回值为"+method.invoke(example,168));
                    }
                    else if("protectedMethod".equals(method.getName())){
                        System.out.println("返回值为"+method.invoke(example,"7",5));
                    }
                    else if("privatedMethod".equals(method.getName())){
                        Object[] parameters = new Object[]{new String[]{"M","W","Q"}};
                        System.out.println("返回值为"+method.invoke(example,parameters));
                    }

                }catch (Exception e){
                    System.out.println("在执行方法时抛出异常下面执行setAccessible()方法");
                    method.setAccessible(true);
                    isTurm = true;
                }
            }
            System.out.println();
        }
    }
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constructor_Annotation {
    String value() default "默认构造方法";
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Field_Method_Parameter_Annotation {
    String describe();
    Class type() default void.class;
}
public class Record {
    @Field_Method_Parameter_Annotation(describe = "编号",type = int.class)
    int id;

    @Field_Method_Parameter_Annotation(describe = "姓名",type = String.class)
    String name;

    @Constructor_Annotation()
    public Record(){}

    @Constructor_Annotation("立即初始化构造方法")
    public Record(@Field_Method_Parameter_Annotation(describe = "编号",type = int.class)
                              int id,
                              @Field_Method_Parameter_Annotation(describe = "姓名",type = String.class)
                              String name
    ){
        this.id = id;
        this.name = name;
    }

    @Field_Method_Parameter_Annotation(describe = "获取编号",type = int.class)
    public int getId(){
        return id;
    }

    @Field_Method_Parameter_Annotation(describe = "设置编号")
    public void setId(
            @Field_Method_Parameter_Annotation(describe = "编号",type = int.class)
            int id
    ){
        this.id = id;
    }

    @Field_Method_Parameter_Annotation(describe = "编号",type = String.class)
    public String getName(){
        return name;
    }

    @Field_Method_Parameter_Annotation(describe = "设置姓名")
    public void setName(
            @Field_Method_Parameter_Annotation(describe = "姓名",type = String.class)
            String name
    ){
        this.name = name;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩淼燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值