이 코드가 컴파일되지 않는 이유를 이해하려고합니다.
인터페이스를 구현하는 클래스가 있습니다. 마지막 방법은 어떤 이유로 컴파일되지 않습니다.SortedSet을 사용한 Java 캐스트 캐스트 예외
단순히 세트를 세트로 캐스트 할 수 없지만 단일 오브젝트를 올바르게 반환 할 수 있습니다.
누군가이 사실을 설명해 주시겠습니까? 감사.
public class Testing2 {
public SortedSet<ITesting> iTests = new TreeSet<ITesting>();
public SortedSet<Testing> tests = new TreeSet<Testing>();
public ITesting iTest = null;
public ITesting test = new Testing();
// Returns the implementing class as expected
public ITesting getITesting(){
return this.test;
}
// This method will not compile
// Type mismatch: cannot convert from SortedSet<Testing> to SortedSet<ITesting>
public SortedSet<ITesting> getITests(){
return this.tests;
}
}
정확한 컴파일러 메시지를 포함하도록 질문을 편집 하시겠습니까? 편집 : 또한 테스트는 ITesting을 구현하는 것처럼 보입니까? –
예, 죄송합니다. 테스트는 ITesting을 구현합니다 – Marley
http://stackoverflow.com/questions/897935/when-do-java-generics-require-extends-t-instead-of-t-and-is-there-any-down의 복제본처럼 보입니다. –