阿提说说 2022-06-17 23:56 采纳率: 100%
浏览 19
已结题

Class类中isInstance判断数组什么时候结果为true

问题遇到的现象和发生背景

Class类中isInstance的使用

如果此Class对象表示一个数组类,如果指定的Object参数可以通过恒等转换或扩展引用转换转换为数组类的对象,则此方法返回true ;否则返回false 。

除了object是类的对象外,还有什么情况是true;

问题相关代码,请勿粘贴截图

//如果object是数组对象
Integer[] integers = new Integer[0];
System.out.println(Number.class.isInstance(integers)); //false
System.out.println(new Integer[0].getClass().isInstance(integers)); //true

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 一头小山猪 一头小山猪(个人IP)官方账号 2022-06-18 09:10
    关注

    子类对象也可以,就是在解释说明中的可转换的情况,如下情况均为true。

            Father father = new Son();
            System.out.println(father instanceof Father);
            System.out.println(father instanceof Son);
            Son son = new Son();
            System.out.println(son instanceof Father);
            System.out.println(son instanceof Son);
            Father[] fathers = new Son[0];
            System.out.println(new Father[0].getClass().isInstance(fathers));
            System.out.println(new Son[0].getClass().isInstance(fathers));
            Son[] sons = new Son[0];
            System.out.println(new Father[0].getClass().isInstance(sons));
            System.out.println(new Son[0].getClass().isInstance(sons));
    
    

    Father为Son的父类,这个是判断实例的类型,所以主要看等号右边。

    有帮助请采纳,还有不懂的可以继续追问~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月26日
  • 已采纳回答 6月18日
  • 创建了问题 6月17日

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了