Java 如何使用泛型三元组重载构造函数?
我需要创建两个构造函数,第一个应该接受三项作为参数。第二个构造器接收单个项,然后三个项将由其中三个项组成 我想在使用this()时,我很难理解重载构造函数 我收到一个错误,提示“在调用超类型构造函数之前无法引用Trio.item1”Java 如何使用泛型三元组重载构造函数?,java,generics,constructor,overloading,Java,Generics,Constructor,Overloading,我需要创建两个构造函数,第一个应该接受三项作为参数。第二个构造器接收单个项,然后三个项将由其中三个项组成 我想在使用this()时,我很难理解重载构造函数 我收到一个错误,提示“在调用超类型构造函数之前无法引用Trio.item1” 公共类三人组{ //实例数据变量 私人T项目1、项目2、项目3; /** *这是所有三项的构造函数 *@param item1 *@param item2 *@param item 3 */ 公共三人组(T项目1、T项目2、T项目3){ 这个项目1=项目1; 本条第
公共类三人组{
//实例数据变量
私人T项目1、项目2、项目3;
/**
*这是所有三项的构造函数
*@param item1
*@param item2
*@param item 3
*/
公共三人组(T项目1、T项目2、T项目3){
这个项目1=项目1;
本条第2项=第2项;
本条第3项=第3项;
}
/**
*第二个构造函数接受单个项
*然后,三人组将由其中三项组成
*@param itemGroup
*/
公共三人组(T组){
这(第1项、第2项、第3项);
}
对于你的第二个构造函数,我很确定你应该通过一个三人组(而不仅仅是T
)。比如
public class Trio<T> {
//instance data variables
private T item1, item2, item3;
/**
* this a constructor for all three items
* @param item1
* @param item2
* @param item3
*/
public Trio(T item1, T item2, T item3) {
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
}
/**
* This second constructor takes in a single item
* and then the trio will consist of three of these items
* @param itemGroup
*/
public Trio(T itemGroup) {
this(item1, item2, item3);
}
/**
* This second constructor takes in a single item
* and then the trio will consist of three of these items
* @param itemGroup
*/
public Trio(Trio<T> itemGroup) {
this(itemGroup.item1, itemGroup.item2, itemGroup.item3);
}
public Trio(T item) {
this(item, item, item);
}