最近,学到了一个有意思的语法,通过class进行类型转换。先上干货,代码如下:

public class Test {
    public static void main(String[] args) {
        Object o = new int[]{16};
        System.out.println(int[].class.cast(o)[0]);
    }
}

核心之处就在于xxx.class.cast(xxx),xxx代表的就是未知变量。上面的这段代码先将一个整型数组赋给object声明的变量,然后将这个对象再通过int[].class转回int数组类型。

 

相关文章:

  • 2022-12-23
  • 2021-09-28
  • 2021-11-18
  • 2021-11-18
  • 2021-09-04
  • 2022-01-09
猜你喜欢
  • 2022-12-23
  • 2021-11-12
  • 2021-10-02
  • 2022-12-23
相关资源
相似解决方案