2015-01-26 3 views
0

내가 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) { ... } 
} 
+3

정확해야 - 당신이 더 많은 코드를 게시 할 수 있습니까? –

+0

나는 왜 내가 실제로 깨달았다 고 생각한다. 컴파일러가 런타임에 GeneticAlgorithm.class가 어떤 유형인지 알지 못하기 때문입니까? –

답변

1

아무 문제가 없다 그것은 작동합니다.

아래 코드 확인 :

interface MyInterface1 { } 
interface MyInterface2 extends MyInterface1 { } 

class MyClass1 implements MyInterface1 { } 
class MyClass2 implements MyInterface2 { } 
public class Program1 { 

    public static void main(String[] args) { 
     a(MyClass1.class); 
     a(MyClass2.class); 

    } 

    public static void a(Class<? extends MyInterface1> input){ 
     System.out.println("OK"); 

    } 

} 
+0

이것은 내 문제가 아마 제네릭의 다른 곳에서 발생했음을 보여줍니다. –

관련 문제