[英]Object instanceof Object
我想檢查一個對象是否是對象類的實例。
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;
}
我開設了一些超越其他課程的課程:
(樹,鐵,石頭)擴展資源擴展對象
人延伸對象
您不能將instanceof
與Class
對象一起使用。
您可以使用以下方法進行檢查:
type.getClass().isInstance(o)
您可以使用
if(o.getClass() instanceof type.getClass()){
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.