好的,所以我试图创建多个具有相同名称的构造函数。我收到了一个错误,我不知道为什么。我基本上都试图这样做,以便当玩家输入他们的名字时,即使他们没有输入特定的字段,它仍然可以工作比如后缀等等我知道我还是要稍后添加setter和getter,但我只是想让构建器立即设置。知道为什么它给我一个错误吗?
public class Name {
private String firstName;
private String middleInitial;
private String lastName;
private String suffix;
public String playerName(String firstName, String middleInitial, String lastName, String suffix){
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.suffix = suffix;
return suffix + firstName + middleInitial + lastName;
}
public String playerName(String firstName, String lastName){
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.suffix = suffix;
return suffix + firstName + middleInitial + lastName;
}
public String playerName(String firstName, String middleInitial, String lastName){ //this is where the error is. "Duplicate method playerName (String String String) in type Name"
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.suffix = suffix;
return suffix + firstName + middleInitial + lastName;
}
public String playerName(String firstName, String lastName, String suffix){//same error here
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.suffix = suffix;
return suffix + firstName + middleInitial + lastName;
}
答案 0 :(得分:1)
如果你打电话
obj.playerName ("string1", "string2", "string3");
java如何知道这是否是
public String playerName(String firstName, String middleInitial, String lastName)
或
public String playerName(String firstName, String lastName, String suffix)
答案 1 :(得分:0)
让我们把你有错误的构造函数
public String playerName(String firstName, String middleInitial, String lastName) { /* error */ }
现在这是另一个你有同样错误的
public String playerName(String firstName, String lastName, String suffix){ /* same error */ }
这是因为您在两个参数和相同数量的参数中使用相同的 DATA TYPE 。因此,当您使用其中一个时,java将无法决定调用哪一个。 我希望你现在明白这一点......
答案 2 :(得分:0)
重载方法(或构造函数)时,只能使用类型签名不同的方法。因此,只有一个方法(同名)需要三个String
参数。但是,您目前没有构造函数。我认为你真正想要的是创建Name
个实例,也许还有getter和setter,最后覆盖toString
(这样你就可以Name
作为String
)。像,
public class Name {
private String firstName;
private String middleInitial = "";
private String lastName;
private String suffix = "";
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Name(String firstName, String middleInitial, String lastName) {
this(firstName, lastName);
this.middleInitial = middleInitial;
}
public Name(String firstName, String middleInitial, String lastName,
String suffix) {
this(firstName, lastName, middleInitial);
this.suffix = suffix;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleInitial() {
return middleInitial;
}
public void setMiddleInitial(String middleInitial) {
this.middleInitial = middleInitial;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(firstName);
if (sb.length() > 0) {
sb.append(' ');
}
if (middleInitial.length() > 0) {
sb.append(middleInitial).append(' ');
}
sb.append(lastName);
if (suffix.length() > 0) {
sb.append(' ');
sb.append(suffix);
}
return sb.toString();
}
}
示例toString
正在执行String
连接,您的效率可能会降低
@Override
public String toString() {
String str = firstName;
if (str.length() > 0) {
str += " "; // <-- str = str + " ";
}
if (middleInitial.length() > 0) {
str += middleInitial + " ";
}
str += lastName;
if (suffix.length() > 0) {
str += " " + suffix;
}
return str;
}