jpfss

1、 判断对象的类型:instanceOf 和 isInstance
或者直接将对象强转给任意一个类型,如果转换成功,则可以确定,如果不成功,在异常提示中可以确定类型

public static void main(String[] args) {
        Integer i = new Integer(10);
        System.out.println(i instanceof Integer); // 知道类型名

        String parentClass = "java.lang.String";
        try {
            Class<?> clazz = Class.forName(parentClass);
            String str = new String("111");
            System.out.println(str.getClass().isInstance(clazz.newInstance())); // 不知道类型名,动态加载类型名
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        String str = "222";
        System.out.println((Integer)str);
    }
原文地址:https://blog.csdn.net/miracle_8/article/details/80404537

分类:

技术点:

相关文章:

  • 2021-11-20
  • 2022-01-03
  • 2021-08-11
  • 2021-09-29
  • 2021-12-01
  • 2021-05-13
  • 2021-12-15
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案