简体   繁体   English

对象实例

[英]Object instanceof Object

I want to check if an object is instance of an objects class. 我想检查一个对象是否是对象类的实例。

public Stack<Object> getObject(int x, int y, int x2, int y2, Object type, Stats stats){
    Stack<Object> ob = new Stack<Object>();
    for(Object o : stats.map){

        if(o instanceof type.getClass()){//Not working 

            if((!((o.x < x && o.x < x2) || (o.x * o.size > x && o.x * o.size > x2)) && 
                !((o.y < y && o.y < y2) || (o.y * o.size > y && o.y * o.size > y2)))){
                ob.push(o);
            }
        }
    }
    return ob;
}

I have made some classes that exctends other classes: 我开设了一些超越其他课程的课程:

(Tree, Iron, Stone) extends Resouce extends Object (树,铁,石头)扩展资源扩展对象

Person extends Object 人延伸对象

  • If type.getClass() = Resource: Tree, Iron and Stone is supposed to return true. 如果type.getClass()=资源:树,铁和石头应该返回true。
  • If type.getClass() = Tree: Tree is supposed to return true and Iron and Stone is supposed to return false. 如果type.getClass()= Tree:树应该返回true,而Iron和Stone应该返回false。

You cannot use instanceof with Class objects. 您不能将instanceofClass对象一起使用。

You can check it using: 您可以使用以下方法进行检查:

type.getClass().isInstance(o)

您可以使用

if(o.getClass() instanceof type.getClass()){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM