java instanceof Class<?> 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13308102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
instanceof Class<?> parameter
提问by マルちゃん だよ
I'm trying to write a method to return all objects that match the class it gets as a parameter:
我正在尝试编写一个方法来返回与它作为参数获取的类匹配的所有对象:
public class Scenario extends View {
...
private Actor[] actors = new Actor[1024];
...
public Actor[] getActors(Class<?> cls) {
//Count actors corresponding to class cls
int cnt = 0;
for (int i = 0; i<actorsCount; i++)
if (actors[i] instanceof cls) cnt++;
//Build a new array;
Actor[] clsActors = new Actor[cnt];
//Fill it
for (int j = 0, k=0; j<cnt; k++)
if (actors[k] instanceof cls)
clsActors[j++] = actors[k];
return clsActors;
}
}
However, I'm getting an error: "- Incompatible operand types boolean and Class<capture#1-of ? extends Scenario>"
但是,我收到一个错误:“- 不兼容的操作数类型 boolean 和 Class<capture#1-of ? extends Scenario>”
'Actor' is extended by my sprites, say Bird, Hero, etc. The idea is, for example, to get a list of all Birds on the Scenario at a given time for some calculations.
'Actor' 由我的 sprites 扩展,比如 Bird、Hero 等。例如,这个想法是在给定时间获取场景中所有鸟类的列表以进行一些计算。
Any idea what's going on here? How to test if a given object is an instance of a given class?
知道这里发生了什么吗?如何测试给定对象是否是给定类的实例?
回答by Konrad H?ffner
import java.util.Arrays;
public class Main
{
static class Actor {}
static class Frog extends Actor {@Override public String toString() {return "I'm a frog";}}
static class Lizard extends Actor {@Override public String toString() {return "I'm a lizard";}}
private static Actor[] actors;
public static Actor[] getActors(Class<?> cls) {
//Count actors corresponding to class cls
int cnt = 0;
for (int i = 0; i<actors.length; i++)
if (cls.isInstance(actors[i])) cnt++;
//Build a new array;
Actor[] clsActors = new Actor[cnt];
//Fill it
for (int j = 0, k=0; j<cnt; k++)
if (cls.isInstance(actors[k]))
clsActors[j++] = actors[k];
return clsActors;
}
public static void main(String[] args)
{
actors = new Actor[] {new Frog(), new Lizard()};
System.out.println(Arrays.toString(getActors(Frog.class)));
}
}
Output:
输出:
[I'm a frog]
Edit: More elegant version of getActors() using a List:
编辑:使用列表更优雅的 getActors() 版本:
public static Actor[] getActors(Class<?> cls) {
LinkedList<Actor> chosenActors = new LinkedList<Actor>();
for(Actor actor: actors) if(cls.isInstance(actor)) chosenActors.add(actor);
return chosenActors.toArray(new Actor[0]);
}
回答by Costi Ciudatu
Try this:
试试这个:
cls.isInstance(yourObject)
instead of using the instanceof
operator, which can only be used if you know the class at compile time.
而不是使用instanceof
运算符,它只能在您在编译时知道类时使用。
回答by Michael Borgwardt
instanceof
can only be used with class literals. You need to use Class.isInstance()
, i.e
instanceof
只能与类文字一起使用。你需要使用Class.isInstance()
,即
if (cls.isInstance(actors[k]))