我在課下。從泛型類型中檢索特定的類類型?
public class BaseRequest<T extends SomeObject> implements Serializable {
/**
* serial version UID
*/
private static final long serialVersionUID = 2602178514139825116L;
private Class<T> classType;
public BaseRequest(Class<T> classType){
this.classType=classType;
}
public Class<T> getClassType() {
return classType;
}
}
Now SomeObject的子類如下。
SomeObjectOne.java
SomeObjectTwo.java
SomeObjectThree.java
客戶端實例化如下類。
BaseRequest request = new BaseRequest(SomeObjectTwo.class);
現在在服務方法中,我必須獲取用戶使用get方法傳遞的類類型。
public void someServiceMethod(BaseRequest<T> request){
//here i need to get the corresponding exact class type using getter.
Class requestedDomainObjectType = request.getClassType();
//Now how can i type cast it to respective class type?
//Is it possible to get exact passed class type?
//I need to get wrapped .class from requestedDomainObjectType
//from requestedDomainObjectType i should get SomeObjectOne.class/SomeObjectTwo.class
}
您的問題不清楚 - 部分原因是您在構建'request'時使用了原始類型。你是否真的想要使用'BaseRequest'? –
請編輯你的問題,然後才能更清楚。 (我甚至不清楚,這將編譯...) –
喬恩,我需要獲得包含Class requestedDomainObjectType在我的問題.class。 – user755806