다음 코드에서 R은 Appendable을 확장하므로 R이 예상되는 Appendable을 반환 할 수 없습니까? 다음 코드에서부터Java에 한정된 일반 공분산
/**
* Produces an R, to which a T has been semantically appended,
* whatever that may mean for the given type.
*/
interface Appendable <R, T>
{
/**
* Append is not expected to modify this Appendable,
* but rather to return an R which is the result
* of the append.
*/
R append(T t);
}
interface PluralAppendable <R extends Appendable<R, T>, T>
extends Appendable<R, T>
{
default R append(T... els)
{
// Easier to debug than folding in a single statement
Appendable<R, T> result = this;
for(T t : els)
result = result.append(t);
/* Error: Incompatible types.
Required: R
Found: Appendable<R, T> */
return result;
}
}
어떻게 인터페이스에서 메서드를 정의 할 수 있습니까? 코드가 컴파일되지 않습니다. – sanbhat
그래서 그냥'return (R) result; '처럼 캐스팅하십시오. 좋아, 나는이 권리를 이해하고 캐스트가 유효하다는 것을 확실히하고 싶었다. 'R result = this;'에서와 같이 선언 결과를 의미하는 경우 "호환되지 않는 유형 : 필수 R, PluralAppendable을 찾았습니다. PluralAppendable이 R을 확장하지 않기 때문에 의미가 있습니다. Appendable –
인터페이스의 "기본"수정 자 및 메소드 본문은 Java 8에서 사용할 수 있습니다. 이는 –