[英]How to pass a class type as parameter and cast to it?
為什么不能將對象轉換為通過參數提供的類類型?
public static checkInstanceOf(Object obj, Class<?> type) {
return obj instanceof type; //error: type cannot be resolved to a type
}
我想創建一個實用程序方法,該方法必須在更詳細的例程中使用此類的instanceof檢查。 我要改變什么?
我希望能夠這樣調用:
MyUtil.checkInstanceOf(new String(""), MyClass.class);
您要使用Class
類的isInstance
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.