简体   繁体   中英

Java Generics extending List

I have the following classes:

public interface ServiceSynchronizableEntity {
    ....
}

public class BaseListResponse<T> {
    ....
}

public class BaseSynchronizableListResponse<T extends ServiceSynchronizableEntity> extends BaseListResponse<T> {
    ....
}

public class MySecondClass implements ServiceSynchronizableEntity {
    ....
}

public class MyFirstClass extends BaseSynchronizableListResponse<MySecondClass> {
    ....
}

public class What<S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity>> {

}

Then I use it as:

What what = new What<MyFirstClass>();

When I want to use MyFirstClass as a type parameter which extend BaseListResponse<ServiceSynchronizableEntity>> it shows me

Main.java:26: error: type argument MyFirstClass is not within bounds of type-variable S
    What what = new What<MyFirstClass>();
                         ^
where S is a type-variable: S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity> declared in class What

What is wrong?

Edit: One class was missing. Look at: http://ideone.com/D4snKP

Thanks!

The solution I previously posted (now deleted) was similar in that it required a change to the generic signature.

The important change is going from:

class What<S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity>> {}

to:

class What<S extends BaseSynchronizableListResponse<? extends ServiceSynchronizableEntity>> {}

See http://ideone.com/wyXvcl for the fixed version

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM