나는 제네릭 클래스가 있습니다혼란, 자바
public class Test<T> {
private Test<? extends T> myInstance;
public Test<? extends T> getInstance() {
return myInstance;
}
public void setInstance (Test<? extends T> argType) {
this.myInstance = argType;
}
}
을 그리고 난 내 클래스 계층 관계에있는 두 개의 클래스가 있습니다
public abstract class Alphabet {
//code here
}
및
public class A extends Alphabet{
public A() {
super();
System.out.print("This is A call");
}
}
마지막으로 나는이를 내가 저장하고 싶은 클래스는 특정 타입의 Generic 클래스 Test를 만들고 Object -> A의 새로운 인스턴스를 설정한다. setInstance()
방법 : 나는 tAlphabet.setInstance(new A());
내 제네릭 클래스의 문제는 무엇인가 라인에서 컴파일 오류를 가지고있다 그러나
public static void main(String[] args) {
List<Alphabet> list = new ArrayList<Alphabet>();
Test<Alphabet> tAlphabet = new Test<Alphabet>();
tAlphabet.setInstance(new A()); //Here is compilation ERROR
}
?
'A'는'Alphabet'을 확장하지만'setInstance'는'Test'의 인스턴스를 필요로합니다. – Bubletan
'Test extends T>'은'T'를 확장하는 제네릭 형식 인수로'Test'의 인스턴스를 나타냅니다. 예를 들어'Test '의 인스턴스는 'A'가'T' ('Alphabet ')를 확장하므로'Test extends T>'의 인스턴스입니다. – Bubletan