내가 2 인터페이스와 2 개 클래스가 : 나는 Class
객체는 MyInterface2
의 적어도 모든 클래스를 포함하는 (적어도 타입 MyInterface1
이 될 수 있도록 클래스를 지정할 수 있도록하려면클래스 객체에 상한 제네릭을 제공하는 방법은 무엇입니까?
interface MyInterface1 { ... }
interface MyInterface2 extends MyInterface1 { ... }
class MyClass1 implements MyInterface1 { ... }
class MyClass2 implements MyInterface2 { ... }
을, 권리?). 나는 그것이 Class<? extends MyInterface1>
와 함께 일할지도 모른다라고 생각했다. 그러나 이는 MyInterface1
을 직접 구현하고 MyInterface2
이 아닌 클래스 만 허용합니다.
보다 구체적인 구현은 다음과 같습니다
public interface Algorithm<E extends Solution<E>> { ... }
public interface EvoAlgorithm<E extends Solution<E>> extends Algorithm<E> { ... }
public class GeneticAlgorithm<E extends Solution<E>> implements EvoAlgorithm<E> { ... }
public class TSPNNAlgorithm implements Algorithm<TSPSolution> { ... }
public enum AlgorithmType {
//**this is where the compiler error is occuring**
GENETIC(GeneticAlgorithm.class),
//**no compiler error here**
NEAREST_NEIGHBOUR(TSPNNAlgorithm.class);
private AlgorithmType(Class<? extends Algorithm<?>> algorithmClass) { ... }
}
정확해야 - 당신이 더 많은 코드를 게시 할 수 있습니까? –
나는 왜 내가 실제로 깨달았다 고 생각한다. 컴파일러가 런타임에 GeneticAlgorithm.class가 어떤 유형인지 알지 못하기 때문입니까? –