2012-07-11 2 views
3

다음 코드가 작동하지 않는 이유를 설명 할 수 있습니까? 나는 이런 식으로 썼다면일반 오류 : 클래스가 인수에 적용되지 않습니다.

The method getAliasForEntityClass(Class<? extends Entity>) in the type EntityManager is not applicable for the arguments 
(Class<capture#1-of ? extends Textable>) 

, 나는 오류가 없었어요 :

public class TestGeneric { 

    EntityManager entityManager = new EntityManager(); 

    public <T extends Entity & Textable> void doAction(T obj) { 
     entityManager.getAliasForEntityClass(obj.getClass()); 
    } 
} 

class EntityManager { 

    public String getAliasForEntityClass(Class<? extends Entity> clazz) { 
     return clazz.getCanonicalName(); 
    } 
} 

interface Entity { 
    Long getId(); 
} 

interface Textable { 
    String getText(); 
} 

템플릿 <T extends Entity & Textable><T extends Textable & Entity>을 의미

public class TestGeneric { 

    EntityManager entityManager = new EntityManager(); 

    public <T extends Textable & Entity> void doAction(T obj) { 
     entityManager.getAliasForEntityClass(obj.getClass()); 
    } 
} 

class EntityManager { 

    public String getAliasForEntityClass(Class<? extends Entity> clazz) { 
     return clazz.getCanonicalName(); 
    } 
} 

interface Entity { 
    Long getId(); 
} 

interface Textable { 
    String getText(); 
} 

나는 다음과 같은 오류를 받고 있어요 T Entity 및 Textable 인터페이스를 구현해야합니다. 이 예제에서 인터페이스의 위치가 중요한 이유는 무엇입니까?

답변

2

이유는 T의 지우기가 Y 운드의 가장 왼쪽 유형이라는 것입니다. getClass() 문서에서 :

The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called.

JLS는 (here 참조) "타입의 변수를 소거가 좌측의 경계의 소거 인"것으로 지정한다. 따라서 obj.getClass() 유형은 첫 번째 경우에는 Class<? extends Textable>이고 다른 경우에는 Class<? extends Entity>입니다.

관련 문제