简体   繁体   中英

Generic type argument in java generics

Is there a way that I can pass a generic type to a generic class like

interface Wrapper<T, R> {}

interface Result<T, Wrapper<T, R>> {
    R getResult();
}

Here Result takes Wrapper one of the type with type arguments T and R . getResult method in Result interface returns R .

What I did is below,

interface Result<T, W> {
    <K> K getResult();
}

Is there a better way to do this in java.

You need a separate type argument for a Result interface. Something like this:

interface Result<T, R, W extends Wrapper<T, R>> {
    R getResult();
}

Now you can use W inside the result to represent the Wrapper or T and R to represent wrapper components. It's possible though that W is completely unnecessary for you and having T and R is enough.

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