具有两个参数的通用 class

Generic class with two parameters

这是问题:

Create a generic class named Duo that has two parameters, A and B. Declare a variable named first of type A, and the second variable named second of type B. Create a constructor that accepts these two parameters. In the constructor, assign these parameters respectively to the declared variables.

这是我能想到的解决方案:

public class Duo<T> {

    T first;
    T second;

    public Duo(T one , T two) {
        this.first = one;
        this.second = two;
    }

}

然后,当我进行下一个问题时,我卡住了。这是问题:

  1. Use the Duo class in Question 4 to declare and create two objects as follows :

    a) First object called sideShape consist of respectively String type and Integer type.

    b) Second object called points consists of two Double types.

我很困惑。如何用两个参数调用泛型 class?

这是我的方法:

Duo <Object> sideShape = new Duo();  

我说的对吗?

如果不行还请大家指出错误。我真的迷路了

您需要两种类型的仿制药:

public class Duo<P,Q> {

P first;
Q second;

public Duo(P one , Q two) {
    this.first = one;
    this.second = two;
}
}

以后可以使用如下:

Duo<String, Integer> duo = new Duo<>("Hello", 5);