2017-01-25 1 views
1

내가 (방법 체인이 많이 발생)를 유창함 인터페이스 API의 일부로서이 세 가지 인터페이스를 가지고 선언함으로써Java에서 일반적인 확장 인터페이스 메소드를 재사용하는 방법은 무엇입니까?

interface VerifierA extends VerifierC { 

     VerifierA method1(); 
     // other methods elided 
    } 

    interface VerifierB extends VerifierC { 
    VerifierB method1(); 
     // other methods elided 
    } 

    interface VerifierC { 
    VerifierA commonMethod(); 
    } 

commonMethod()VerifierA 나는 할 수있는 다음 체인 방법 commonMethod().method1()하지만 commonMethod().method2();

어떻게하면 commonMethod()에 넣을 수 있습니까? VerifierC 필요에 따라 VerifierA 또는 VerifierB 모두로 되돌아 갈 수 있습니까?

+1

을 나는이 이미 http://stackoverflow.com/questions/1069528/method-chaining-inheritance-don-t-play-well-에 요청합니다 생각 함께 또는 http://stackoverflow.com/questions/9655335/method-chaining-how-to-use-getthis-trick-in-case-of-multi-level-inheritance – pringi

+0

참조 된 답변을 읽었을 때, 두려운 인터페이스로만 구성된 내 상황에 적용 할 수 없을까 ... – Andrejs

+0

commonMethod가 VerifierC를 반환하고 VerifierB를 오버라이드하여 VerifierB를 반환하고 VerifierA를 사용하여 VerifierA를 반환하게하는 이유는 무엇입니까? – david

답변

2

당신은 제네릭과 그것을 해결할 수 :

interface VerifierA extends VerifierC<VerifierA> { 
    VerifierC<VerifierA> method1(); 
} 

interface VerifierB extends VerifierC<VerifierB> { 
    VerifierC<VerifierB> method1(); 
} 

interface VerifierC<T> {  
    T commonMethod(); 
} 
+0

+1 나는 똑같은 대답을하려고했다. VerifierB # method1의 반환 유형이 'VerifierC '이 아니어야합니까? –

+0

인터페이스'VerifierB'에서'method1()'에''이 있어야합니까? – Andrejs

+0

@ luc14n0 확실히. 감사! –

관련 문제