같은 인터페이스를 공유하는 모든 클래스의 클래스를 가질 수있는 비교자를 만들고 싶습니다. 그러나 다음 코드는 작동하지 않습니다.Comparator를 같은 인터페이스의 클래스와 연결하는 방법은 무엇입니까?
public class MainApp {
public void test() {
ComparatorChain<TestInterface> comp = new ComparatorChain<>();
comp.addComparator(new TestAComparator());
// ERROR: The method addComparator(Comparator<TestInterface>) in the type ComparatorChain<TestInterface> is not applicable for the arguments (MainApp.TestAComparator)
}
//more comparators for each class implementing TestInterface
class TestAComparator implements Comparator<TestClassA> { //TestClassA implements TestInterface
@Override
public int compare(TestClassA o1, TestClassA o2) {
return 0;
}
}
}
public interface TestInterface {
}
//more classes that implement this interface
public class TestClassA implements TestInterface {
}
무엇이 잘못 되었나요? 어떻게이 비교를 할 수 있습니까?
'ComparatorChain' 클래스의 코드를 공유 할 수 있습니까? –
저는 여기서 ComparatorChain이 필요하다고 생각하지 않습니다. ComparatorChain은 비교기 목록에서 각 비교기가 0이 아닌 값을 반환 할 때까지 실행합니다. – BretC
또는이 지원 기능이 내장 된 Java 8을 사용할 수 있습니다. –