다음 코드에서 명령을 C로 명시 적으로 변환해야하는 이유는 무엇입니까? 명령은 Runnable과 Describable을 모두 구현합니다. 내가 생각하고있는두 개의 인터페이스로 제네릭 형식 매개 변수를 선언 할 때 불필요한 캐스트를 피하는 방법
@Test
public <C extends Runnable & Describable> void testMapOfCommands() throws Exception
{
Map<String, C> commands = Maps.newHashMap();
for(Commands command : Commands.values())
{
commands.put(command.name(), (C) command);
}
//Use commands here (not relevant to my question):
//CommandLineParser.withCommands(commands).parse("commit");
}
private enum Commands implements Runnable, Describable
{
commit
{
@Override
public void run()
{
System.out.println("COMMIT");
}
@Override
public String description()
{
return "Commits something";
}
};
}
한 가지 해결 방법은 Runnable를하고 설명 가능한 모두 확장 ICommand의를 소개하는 것입니다 :
public interface ICommand extends Runnable, Describable{}
하지만 난 이미 두 가지 유형이있을 때 새로운 유형을 소개하는 것을 피하기 위해 노력하고있어 쉽게 사용할 수 있으며 이미 Command 클래스가 조금 더 복잡합니다. 내가 빨대를 쥐고 있니?
이들은 인터페이스입니다. – jontejj
먼저 전체 수업을 게시하십시오. 둘째, Runnable과 같은 인터페이스에는 확장이 아닌 구현을 사용해야합니다. – Antimony
코드를 약간 업데이트했습니다. 모든 관련 코드가 게시 된 것 같습니다. 어떤 부분이 빠졌습니까? – jontejj