문제

I am trying to read and understand some Java code. Here it is:

protected LoadTarget<? super PopulationLoadContext> createTarget(PopulationLoadContext context) {
    return createTransactionalTargetGroup(RiskScoresTables.All_Tables);
}

What does the <? super PopulationLoadContext> mean?

도움이 되었습니까?

해결책

In Java, that is a lower-bounded wildcard in generics, which stands for PopulationLoadContext or any superclass of that class.

It could be a LoadTarget<PopulationLoadContext>, or it could be a LoadTarget<Object>, or anything in between (if in between classes exist).

다른 팁

What Is It?

As stated by rgettman it's a generic type LoadTarget with a type argument set to a lower-bounded wildcard.

What Does It Allow?

At runtime a LoadTarget variable can be substituted (set) with a value of same type where the type argument is PopulationLoadContext or an ancestor.

Why?

To allow flexibility - a family of different instantiated types can be substituted for one another and work fine. Within LoadTarget, the type parameter is used for method parameters (input) or for upper type bounds of internally instantiated generic types. In these cases a broader type is substitutable for the original type because it is more accommodating. The generic type is said to have contravariance with it's type parameter

More generally (not just Java speaking), if you want to get a bigger picture, it is also called covariance / contravariance: http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29 see Java section:

"given a List<? extends Foo>, then an element can be retrieved and safely assigned to a Foo type (covariance). Given a List<? super Foo>, then a Foo object can be safely added as an element (contravariance)."

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top