JAVA 反射疑问(新手自学求教)

aaron222hgh 2021-10-16 00:10:06


import java.lang.reflect.*;
 
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) {
                // 如果该成员变量的访问权限为private,则抛出异常,即不允许访问
                try {                    
                    if (i == 2) {
                        // 通过执行默认没有参数的构造方法创建对象
                        example2 = (Example_01)constructor.newInstance();
                    }else if (i == 1) {
                        // 通过执行2个参数的构造方法创建对象
                        example2 = (Example_01)constructor.newInstance("2", 6);
                    }else {
                        // 通过执行多个参数的构造方法创建对象
                        Object[] parameters = new Object[] {new String[] {"222", "666", "888"}};
                        example2 = (Example_01)constructor.newInstance(parameters);
                    }
                } catch (Exception e) {
                    System.out.println("在创建对象时抛出异常,下面执行setAccessible()方法");
                    constructor.setAccessible(true);         // 设置为允许访问
                }            
            }    
            if (example2 != null) {
                example.print();
                System.out.println();
            }
        }    
    }

中以下两处的目的?

  1. Example_01 example = new Example_01("10", "20", "30");

  2. Example_01 example2 = null;

...全文
188 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
scmod 2021-10-16
  • 打赏
  • 举报
回复 1

new出来不是为了下面

if (example2 != null) {
    example.print();
    System.out.println();
}

这里能调用example.print()
同样Example_01 example2 = null;也只是为了while循环后能拿到example2判断他!= null
没啥别的目的啊?

「LLM那些事」系列第 4 篇《上下文窗口的边界》,文章连接:https://blog.csdn.net/houwenjin/article/details/163999753。 演示什么:在「预测」Sheet 的黄色格子里输入一句话(默认「来泡一杯」),四个「模型」——分别只统计最后 1 / 2 / 3 / 4 个字的 n-gram 查表——同时预测下一个字。同一个输入,看的上下文越长,候选越少、预测越确定: ┌────────────────┬──────────┬───────────────┬──────┐ │ 只看最后几个字 │ 用的前缀 │ 候选下一字数 │ 预测 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 1 个 │ 杯 │ 3(茶/子/水) │ 模糊 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 2 个 │ 一杯 │ 2(茶/水) │ 收窄 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 3 个 │ 泡一杯 │ 1(茶) │ 确定 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 4 个 │ 来泡一杯 │ 1(茶) │ 确定 │ └────────────────┴──────────┴───────────────┴──────┘

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧