다음 인라인 Comparator가 있습니다.java의 Typechecking 및 generics가 경고를 생성합니다.
Comparable
is a raw type. References to generic typeComparable<T>
should be parameterized
그리고 제안 내가 대신 유형을 매개 변수화하는 경우 : 다음과 같은 경고를 생성
private static class SampleSorter implements Comparator<SampleClass>{
public int compare(SampleClass o1, SampleClass o2) {
if (o1 instanceof Comparable) {
return ((Comparable) o1).compareTo(o2);
} else if (o2 instanceof Comparable) {
return -((Comparable) o2).compareTo(o1);
}
return 0;
}
}
if (o1 instanceof Comparable) {
return ((Comparable<SampleClass>) o1).compareTo(o2);
가 그럼 난 경고를 얻을 ...
Type safety: Unchecked cast from
SampleClass
toComparable<SampleClass>
내가 유형 검사 할 경우 : 나는 오류 다음 얻을
if (o1 instanceof Comparable<SampleClass>) {
return ((Comparable<SampleClass>)o1).compareTo (o2);
을 :
Cannot perform instanceof check against type
Comparable<SampleClass>
. Use the formComparable<?>
instead since generic type information will be erased at runtime
그리고 다시, 나는 오류 메시지의 조언을 따르는 경우 :
if (o1 instanceof Comparable<?>) {
return ((Comparable<?>)o1).compareTo (o2);
는이 오류를 얻을 :
지금 내가 것이며 진행하는 방법을 몰라, 내가 경고 - 오류없는 정말 prefere 코드입니다. 원하는 동작으로 경고없는 코드를 생성하려면 어떻게합니까?The method
compareTo(capture#4-of ?)
in the typeComparable<capture#4-of ?>
is not applicable for the arguments(SampleClass)
을 시도해보십시오 O1는 비교할와 O2가없는 경우 어떻게됩니까? 둘 모두 Comparable이 될 수없는 것 같습니다. – oers