简体   繁体   English

传递字符串时,Java构造函数varargs发生冲突

[英]Java constructor varargs conflict when passing string

I have an issue with one of my class. 我和我的一个班级有问题。 I'm using a "varargs" constructor for unknown number of parameter. 我正在使用“varargs”构造函数来获取未知数量的参数。

public Groupe(String...nom){        
    for(String item:nom){   
        this.nom.add(item.toLowerCase());
    }
}

public Groupe(String nom){      
    String[] list =nom.split(",");
    for(String s : list){           
        this.nom.add(s.toLowerCase());
    }
}

The first constructor is called...that's fine, but there is a conflict when passing only ONE parameter with the second contructor. 第一个构造函数被调用......这很好,但是当第二个构造函数只传递一个参数时会发生冲突。 I would like to use the second constructor when passing only one string, and the first if 2 and more parameters. 我想在传递一个字符串时使用第二个构造函数,并且第一个if 2和更多参数。

I'd want to handle this new Groupe("Foo,Bar"); 我想要处理这个新的Groupe(“Foo,Bar”);

This is where I call it. 这就是我所说的。 I suspect the "error" comes from there 我怀疑“错误”来自那里

public void reserver(String...nom){
    Groupe gr = new Groupe(nom);
    passager.add(gr);       
}

I don't pass a String, but a Varargs (tab?)... 我不传递字符串,而是传递Varargs(tab?)...

It should be fine, with the caveat that null can be converted to either String[] or String : 它应该没问题,但需要注意的是null可以转换为String[]String

public class Test {

    public Test(String single) {
        System.out.println("Single");
    }

    public Test(String... multiple) {
        System.out.println("Multiple");
    }

    public static void main(String[] args) {
        new Test("Foo"); // Single
        new Test("Foo", "Bar"); // Multiple
        new Test(); // Effectively multiple
        // new Test(null); // Doesn't compile - ambiguous
        new Test((String) null); // Single
    }
}

EDIT: Now that you've shown us the calling code, that's definitely the problem: 编辑:既然你已经向我们展示了调用代码,那肯定是问题所在:

public void reserver(String...nom){
    Groupe gr = new Groupe(nom);
    passager.add(gr);       
}

Here, the type of nom is String[] - so it will always call the first constructor. 这里, nom的类型是String[] - 所以它总是调用第一个构造函数。 You've got an array of strings there - under what circumstances do you want to call the second constructor? 你有一个字符串数组 - 在什么情况下你想调用第二个构造函数?

To be honest, given that the two constructors act significantly differently, I would actually make both constructors private, and provide static methods: 老实说,鉴于两个构造函数的行为有很大不同,我实际上会将两个构造函数都设置为私有,并提供静态方法:

public static Groupe fromStringArray(String... nom)

public static Groupe fromCommaSeparatedString(String nom)

Then it will be absolutely clear what you're expecting in each case. 那么在每种情况下你都会非常清楚你期待的是什么。

Maybe this can be a solution: 也许这可以是一个解决方案:

public Groupe(String...nom){       
    if (nom.length == 1) {
        add(nom[0].split(","));
    } else {
        add(nom);
    }
}

private void add(String[] list) {
    for(String s : list){           
        this.nom.add(s.toLowerCase());
    }
}

The varargs part can be empty. varargs部分可以是空的。 So you can get what you want with 所以你可以得到你想要的东西

public Groupe(String nom){
  String[] list = nom.split(",");
for(String s : list){           
    this.nom.add(s.toLowerCase());
}

 public Groupe(String nom1, String nom2, String...nom){   
this.nom.add(nom1);
this.nom.add(nom2);     
for(String item:nom)    
    this.nom.add(item.toLowerCase());
}

You could also, of course, use one ctor with an if statement on the length of the input array, splitting out cases 0 (not handled with the code above), 1, and > 1. 当然,您也可以在输入数组的长度上使用一个带有if语句的ctor,将case 0分开(不用上面的代码处理),1和> 1。

public class OverloadVarArgs {

public static void main(String... args){
    OverloadVarArgs a = new OverloadVarArgs("One Argument");
    OverloadVarArgs b = new OverloadVarArgs("Two", "Arguments");
    OverloadVarArgs c = new OverloadVarArgs("One, Argument");

}

public OverloadVarArgs(String a){
    System.out.println("Constructor 1");
}

public OverloadVarArgs(String... a){
    System.out.println("Constructor 2");
}
}

Output: 输出:

Constructor 1 构造函数1

Constructor 2 构造函数2

Constructor 1 构造函数1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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