gpt4 book ai didi

java android 获取类

转载 作者:行者123 更新时间:2023-12-01 13:46:41 25 4
gpt4 key购买 nike

Class<?> b1;
b1 = GS.scr_check_freebox(x, y-GS.ss, this);

if ( b1.getClass().equals(BLK.class) || b1.getClass().equals(STN.class) )
{
if (b1.falling == true)
{
scr_cascade_fall(scr_check_freebox(x, y-GS.ss, this));
return (true);
}
}

函数“scr_get_freebox”搜索 BLK 和 STN 的所有实例,并返回它发现的第一个完全或部分存在于您传递给它的坐标左上角的盒子内的实例,但它将从搜索中排除你传递的“这个”。它并不重要,只是它返回 null 或 BLK 实例或 STN 实例。

在第 5 行给出错误,其中显示“if (b1.falling == true)”问题是,这两个类(BLK 和 STN)都有一个称为落下的 boolean 变量。编译器在这里只是愚蠢的。

如何消除这个错误?有没有特殊的方法来使用 getClass() 或 instanceof ?

最佳答案

您已声明b1Class<?> 。所以当然b1.falling == true是一个编译错误 Class<?>没有falling字段。

你想要的是改变Class<?> b1;父类(super class) BLK/STN因此,在最坏的情况下,如果没有除 Object 之外的其他父类(super class),它可以保存这些类的实例。 (后来使用强制转换来访问字段):

Object b1;

关于java android 获取类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20316367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com