[英]“Interface type cannot be statically allocated” on multiple class-type variables after @property
[英]Java: Class-type parametrized by interface implementations
假設我有一個界面
interface I {}
及其兩個實現, class A implements I {}
, class B implements I {}
現在,我想編寫一個通用方法,該方法接受以“ Implements interface I”為邊界的類類型參數
boolean <T> isOK ( Class<T extents I> cl ) {
switch ( cl ) {
case A.class: return true ;
case B.class: return false;
}
}
怎么做?
我認為您的意思是:
public <T extends I> boolean isOK(Class<T> cl) {
...
}
泛型類型的限定符位於聲明泛型類型的位置,即第一個三角括號,而不是第二個。
同樣,您不能打開Class
,您必須使用其他方法來檢查它,例如if
語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.