与参数具有相同原始类型的构造函数

huangapple go评论151阅读模式
英文:

Constructors with the same primitive type as parameter

问题

我有一个作业,要求我创建一个名为“Node Center”的数据类,供我的其他类使用,例如LinkedList、Stack和Queue类。我正在为Data类创建构造函数,以便在LinkedList、Stack和Queue类中使用。直到我实现Queue的构造函数之前,我都没有遇到任何问题。在Data类的Stack部分中,我已经创建了一个以int作为构造函数参数的public Data构造函数。当我尝试为Queue创建另一个以int作为构造函数参数的public Data构造函数时,我收到错误消息:Data(int)在Data中已经定义。以下是我的Stack代码:

/*
    使用数组的栈
    */
    int size; //初始化大小
    int stackArray[]; //初始化数组
    int top; //初始化栈顶

    public Data(int size) //构造函数
    {
        this.size = size;
        this.stackArray = new int[size];
        this.top = -1;
    }

以下是我的Queue代码:

/*
    使用数组的队列
    */
    public int Queue[]; //建立队列数组和变量
    public int front;
    public  int rear;
    public int queueSize;
    public int len;

    public Data(int nQueue)//构造函数
    {
        size =nQueue;
        len = 0;
        Queue = new int[size];
        front = -1;
        rear = -1;
    }

如何修复这个问题,以便我可以有两个具有相同参数类型的构造函数?

英文:

I have an assignment that asks me to create a Data Class as a "Node Center" for all my other classes, such as a class for LinkedList, Stack, & Queue to "feed" on. I'm creating constructors in the Data class to work on the LinkedList, Stack, & Queue classes. I wasn't having any problems until I implemented my constructor for my Queue. In my Stack portion of the Data class, I had already created a public Data constructor with an int as my parameter in the constructor. When I try to create another public Data constructor with an int as my parameter for the Queue, I get the error: Data(int) is already defined in Data. Here is my Stack code:

/*
    STACK WITH AN ARRAY
    */
    int size; //initialize size
    int stackArray[]; //initialize array
    int top; //initialize top

    public Data(int size) //constructor
    {
        this.size = size;
        this.stackArray = new int[size];
        this.top = -1;
    }

And here is my Queue code:

/*
    QUEUE WITH AN ARRAY
    */
    public int Queue[]; //establish queue array and variables
    public int front;
    public  int rear;
    public int queueSize;
    public int len;

    public Data(int nQueue)//constructor
    {
        size =nQueue;
        len = 0;
        Queue = new int[size];
        front = -1;
        rear = -1;
    }

How do I fix this so I can have 2 constructors with the same parameter type?

答案1

得分: 1

它们都是类型为int的构造函数。Java不关心你给int参数起什么名字,所以它们在Java中看起来像是相同的构造函数(至少对Java来说是如此)。由于你不能有两个具有相同参数的构造函数,你有两个选项。

  1. 给其中一个构造函数一个不同的数据类型参数。
  2. 给其中一个构造函数另一个参数(2个参数)。

这两种方法都将使JVM能够区分你的两个构造函数。

英文:

They are both constructors of type int. Java doesn't care about what you name the int argument, so they look like the same constructor (at least to java). Since you cannot have two constructors with the same arguments, you have two options.

  1. Give one of the constructors a different data type argument.
  2. Give one of the constructors another argument. (2 arguments)

Either of these will allow the jvm to tell the difference between your two constructors.

答案2

得分: 1

不能为两个方法或构造函数拥有相同的名称和相同的签名。

在您的情况下,如果您调用new Data(10)来创建Data的实例,编译器将不知道调用哪个构造函数来创建实例。

您可以添加第二个boolean参数来指定要创建的实例类型。

public Data(int size, boolean isQueue)//构造函数
{
  if(isQueue){
    len = 0;
    Queue = new int[size];
    front = -1;
    rear = -1;
  }else{
    this.size = size;
    this.stackArray = new int[size];
    this.top = -1;
  }
}

JLS:§8.4.2 方法签名

英文:

It's not possible to have same name and same signature for two methods or constructors.

In your case if you call new Data(10) to create an instance of Data the compiler won't know which constructor to call to create the instance.

You can have a second boolean argument to mention what kind of instance to be created.

public Data(int size, boolean isQueue)//constructor
{
  if(isQueue){
    len = 0;
    Queue = new int[size];
    front = -1;
    rear = -1;
  }else{
    this.size = size;
    this.stackArray = new int[size];
    this.top = -1;
  }
}

JLS: §8.4.2 Method Signature

答案3

得分: 0

无法在列表中创建具有相同参数数量和类型的两个构造函数。您可以创建队列(Queue)和栈(Stack)的子类,并在各自的子类中为它们定义构造函数。

英文:

You cannot create two constructor having same number of parameters in the list and their types. What you can do is create sub classes for Queue and Stack and define constructor for each of them in respective sub classes.

huangapple
  • 本文由 发表于 2020年7月22日 12:01:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63026650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定