2
我寫一個工廠類,看起來像這樣:檢查一個類型參數是一個特定的接口
public class RepositoryFactory<T> {
public T getRepository(){
if(T is IQuestionRepository){ // This is where I am not sure
return new QuestionRepository();
}
if(T is IAnswerRepository){ // This is where I am not sure
return new AnswerRepository();
}
}
}
,但我怎麼能檢查T
是一種類型的指定interface
?
你不能。將'Class'實例傳遞給'getRepository()'。 –