자바 제네릭 내에서 간 수준의 순환 참조를 만들 수있는 트릭이있다을 참조 :인스턴스화 원형 제네릭 클래스
public static class GenA<A extends GenA<A, B>, B extends GenB<A, B>> {
public B objB;
}
public static class GenB<A extends GenA<A, B>, B extends GenB<A, B>> {
public A objA;
}
내가 실제 개체의 인스턴스를하려고 :
GenA<GenA, GenB> genA = new GenA<>();
을 내가 컴파일 오류를 얻을 :
CircularRef.java:7: error: type argument GenA is not within bounds of type-variable A
GenA<GenA, GenB> genA = new GenA<>();
where A,B are type-variables:
A extends GenA<A,B> declared in class GenA
B extends GenB<A,B> declared in class GenA
CircularRef.java:7: warning: [rawtypes] found raw type: GenA
GenA<GenA, GenB> genA = new GenA<>();
missing type arguments for generic class GenA<A,B>
where A,B are type-variables:
A extends GenA<A,B> declared in class GenA
B extends GenB<A,B> declared in class GenA
CircularRef.java:7: warning: [rawtypes] found raw type: GenB
GenA<GenA, GenB> genA = new GenA<>();
missing type arguments for generic class GenB<A,B>
where A,B are type-variables:
A extends GenA<A,B> declared in class GenB
B extends GenB<A,B> declared in class GenB
CircularRef.java:7: warning: [unchecked] unchecked method invocation: constructor <init> in class GenA is applied to given types
GenA<GenA, GenB> genA = new GenA<>();
required: no arguments
found: no arguments
CircularRef.java:7: warning: [unchecked] unchecked conversion
GenA<GenA, GenB> genA = new GenA<>();
required: GenA<GenA,GenB>
found: GenA
나는 알고 해결 방법 :
0 같은 작품public static class AdapterA extends GenA<AdapterA, AdapterB> { }
public static class AdapterB extends GenB<AdapterA, AdapterB> { }
:
AdapterA adA = new AdapterA();
AdapterB adB = new AdapterB();
adA.objB = adB;
adB.objA = adA;
어떻게 GenA
/GenB
클래스를 직접 인스턴스화 할 수 있습니까?
이들은 abstract
이 아닙니다. 나는 -Xlint:all
도 경고가 없다고 말합니다.
EXTRA 나는))
을 닫습니다 싶은 코드와 지루 많은 사용자에 숨겨진 문제 이상 포함 질문 Defining typed hierarchy with Java generics and type self-referencing을 썼다 일반적인 원형 참조 선언을 선언에 대한 몇 가지 힌트에 있습니다 :
- Creating circular generic references
- Cyclic generic dependenices
- How to get "circular" generics working in Java?
추가 기준 추가하는 UPDATE : 동일한
public static class GenA<A extends GenA<A, B, C>, B extends GenB<A, B, C>, C extends GenC<A, B, C>> {
public B objB;
public C objC;
}
public static class GenB<A extends GenA<A, B, C>, B extends GenB<A, B, C>, C extends GenC<A, B, C>> {
public A objA;
public C objC;
}
public static class GenC<A extends GenA<A, B, C>, B extends GenB<A, B, C>, C extends GenC<A, B, C>> {
public A objA;
public B objB;
}
문제 :
GenA<GenA, GenB, GenC> genA = new GenA<>();
과는 평소와 같이 해석 할 수있다
public static class AdapterA extends GenA<AdapterA, AdapterB, AdapterC> { }
public static class AdapterB extends GenB<AdapterA, AdapterB, AdapterC> { }
public static class AdapterC extends GenC<AdapterA, AdapterB, AdapterC> { }
UPDATE 2 문제 렘 인스턴스화하지 않지만 형의 선언으로, 다음은 실패
GenA<GenA, GenB> genA;
당신은 할 수 없습니다 수 있습니다'도와주지 않을 GENA = 새로운 GENA <>();''도와주지 <도와주지 , GenB <...>>, GenB , GenB <...> >> GENA = 새로운 GENA <>()해야한다 ; ', 결국 무한히 길어질 수 있습니다. –
** @ MadPhysicist **이 문제에 대해 생각했지만 Java Generic 사양에 익숙하지 않은 것은 확실하지 않았습니다 ... – gavenkoa
중첩 된 제네릭 수준을 한 단계 더 추가하면 어떻게됩니까? 원시 형식 오류를 제거 할 수 있습니까? 작동하는 경우 어댑터 클래스를 갖는 것보다 낫습니다. –