한 클래스의 제네릭 유형을 전자와 자식 부모 관계가없는 다른 유형으로 만드는 방법은 무엇입니까?

Sid06

그래서 다음과 같은 인터페이스가 있습니다.

public interface INotifier<T,G>{
   G getNotifications(){
    //does something   
    }
}

인터페이스를 구현하고 G에 대해 다른 일반 유형을 갖는 2 개의 다른 하위 클래스가 있습니다.

다음과 같이 getNotification 메서드를 사용하는 또 다른 클래스 :

public class Orchestrater<K>{
   K notifications = notifier.getNotifications();
}

K를 G와 동일한 유형으로 추론하고 싶습니다. 그러나 Orchestrater를 부모 클래스로 또는 INotifier의 하위 클래스로 리플렉션을 통해 결론을 내릴 수 없습니다. 어떻게하는지 아십니까?

Tmarouane

INotifier#getNotifications(T)반환 유형에 대해 Orchestrator<K>제네릭 유형과 동일한 형식 매개 변수를 갖는 것은 간단해야하며 모두 notifier필드 선언 에 따라 다릅니다 .

다음은 Orchestrator클래스 의 개발 된 샘플입니다 .

public class Orchestrator<K>
{
    private INotifier<Object, K> notifier;

    public K getOrchestratedNotifications() {
        return notifier.getNotifications();
    }
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관