简体   繁体   English

尝试定义这三个构造函数时为什么会出错? 给出的例子

[英]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.

相关问题 当我尝试在库存外部/没有物品的库存中单击时,为什么会出现错误? - Why do I get an error when I try to click outside the inventory / in the inventory where there's no item 为什么在尝试输出数组时会出现异常错误? - Why do I get an exception error when I try output an array? 当我尝试在基类方法中调用子类方法时,为什么会出现错误? - Why do I get an error when I try to call a subclass method inside a baseclass method? 当我尝试保存测试计划时,为什么会出现 NoClassDefFound 错误? - Why do I get a NoClassDefFound error when I try to save my test plan? 当我尝试使用具有相同名称和参数类型的两个方法时,为什么会出现编译错误? - Why do I get a compilation error when I try to have two methods with the same name and parameter type? 当我尝试使用我的jsf自定义标记时,为什么会出现错误“前缀[..]未定义”? - Why do I get error “prefix [..] is not defined” when I try to use my jsf custom tag? 尝试包含Urdu字符串时为什么会出现编译错误? - Why do I get a compilation error when I try to include an Urdu string? 我尝试打开URL时为什么会收到403错误 - Why do I get a 403 error when I try open a URL 为什么会出现编译错误? 使用 try and catch - Why do I get compile error? using try and catch 当我尝试使用 Jsoup 在我的 class 中获取多个文档时,为什么会出现 handshake_failure 错误? - Why do I get a handshake_failure error when I try to get more than 1 Document in my class using Jsoup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM