[英]Why do i get a error when i try to define these three constructors? Examples given
Okay so the problem is, NetBeans is saying the second one is already defined. 好的,问题是,NetBeans表示已经定义了第二个。 These three are my constructors at the top, The whole program is listed in case a set or get method is the error of it.
这三个是我在顶部的构造函数,如果set或get方法是它的错误,则列出整个程序。 So to be clear i am talking about
所以我要说的是
public Dog(String initialName) public Dog(String initialBreed) public Dog(double initialWeight) public Dog(String initialName)public Dog(String initialBreed)public Dog(double initialWeight)
The error shows up on public Dog(String initialBreed). 该错误显示在公共Dog(String initialBreed)上。 Did i misuse the Overload method?
我是否滥用了过载方法? Also i must use the overload method it is mandatory.
我也必须使用过载方法,它是强制性的。
package dog;
import java.util.*;
public class Dog
{
// instance variables
private String name;
private String breed;
private double weight;
public Dog( )
{
name = "no name";
breed = "no breed";
weight = 0.0;
}
public Dog(String initialName)
{
name = initialName;
breed = "no breed";
weight = 0.0;
}
public Dog(String initialBreed){
name = "no name";
breed = initialBreed;
weight = 0.0;
}
public Dog(double initialWeight){
name = "no name";
breed = "no breed";
weight = initialWeight;
}
public void SetDog(String newName, String newBreed, double newWeight)
{
name = newName;
breed = newBreed;
if (newWeight <= 0)
System.out.println("Error: Negative weight.");
else
weight = newWeight;
}
public void setName(String newName){
name = newName;
}
public void setBreed(String newBreed){
breed = newBreed;
}
public void setWeight(double newWeight){
weight = newWeight;
}
public double getWeight(){
return weight;
}
public String getName(){
return name;
}
public String getBreed(){
return breed;
}
} }
The problem is that two of your constructors take the same argument: 问题是你的两个构造函数采用相同的参数:
public Dog(String initialName)
public Dog(String initialBreed){
They both take string
. 他们俩都带
string
。 You can't have two methods with the exact same name and parameters. 您不能拥有两个具有完全相同名称和参数的方法。
Based on what I think you are trying to do, you might want a single constructor that takes all 3 of those parameters: 基于我认为您尝试做的事情,您可能需要一个构造函数来获取所有这三个参数:
public Dog(String initialName, String initialBreed, double initialWeight){
public Dog(String initialName)
{
name = initialName;
breed = "no breed";
weight = 0.0;
}
public Dog(String initialBreed){
name = "no name";
breed = initialBreed;
weight = 0.0;
}
The above two constructor which you defined are not overloaded properly as both are having same header with same type and numbers of arguments. 您定义的上述两个构造函数未正确重载,因为它们具有相同类型和参数数量的相同标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.