Using generic class as type parameter

gadria :

I would like to use a generic class as a type parameter for another generic class.

At first my class definition was like this:

class Foo<T, R extends BaseDto> {}

Then my requirements changed and I had to use a wrapper/holder class for my R type

My attempt so far: (Gives compile-time error: class or interface expected on Bar<R>)

class Bar<T extends BaseDto> {
    private T dto;
    public Bar(T dto) {
        this.dto= dto;
    }
    // getters and setters
}
class Foo<T, Bar<R>> {
    private T entity;
    private R dto;
    // getters and setters
}

I can not use it as Foo<T, Object> because then the clients using this class would not know that they actually need a cast from Object to Bar.

How would I implement such behaviour?

Sweeper :

Instead of changing the class header, replace everywhere in the class where you used R with Bar<R>.

So the class header stays the same:

class Foo<T, R extends CustomClass> 

But let's say you have a field of type R. That needs to be changed to Bar<R>:

Bar<R> someField;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=415137&siteId=1