构造函数 CircularQueue 在类 CircularQueue 中不能应用于给定类型。

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

constructor CircularQueue in class CircularQueue cannot be applied to given types

问题

错误:

QueueTestHarness.java:19: 错误: 无法将类 CircularQueue 中的构造函数 CircularQueue 应用于给定类型;
            testC[1] = new CircularQueue(SIZE);
                       ^
  需要: 无参数
  找到: int
  原因: 实际参数列表和形式参数列表的长度不同
2 个错误

我明白这表示我没有一个接受参数(int)的构造函数,但在我的类中我有一个:

 public Queue(int inMax)
      {
          queue = new Object[inMax];
          int count = 0;
      }

另外 CircularQueueQueue 的子类,这在我的代码中有证据:

public class CircularQueue extends Queue

那么为什么会出现这个错误呢?

注意:我也有一个默认构造函数,似乎运行良好。

英文:

Error:

QueueTestHarness.java:19: error: constructor CircularQueue in class CircularQueue cannot be applied to given types;
            testC[1] = new CircularQueue(SIZE);
                       ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
2 errors

I understand this means I don't have a constructor taking in a parameter (int) but in my class I do:

 public Queue(int inMax)
      {
          queue = new Object[inMax];
          int count = 0;
      }

Also CircularQueue is a subclass to Queue and here's proof of that in my code:

public class CircularQueue extends Queue

So why is this error popping up?

Note: I also do have a default constructor and it seems to work fine.

答案1

得分: 1

朋友,Queue是一个接口,这意味着如果你想在你的类中使用它(抽象),你需要使用implements关键字。

extends关键字用于扩展类... 但Queue是一个接口,你的代码正在尝试扩展Queue,这是错误的。

请使用以下代码:

public class CircularQueue implements Queue

别忘了实现所有Queue方法,或者将你的类声明为抽象类。

还有一件事情,

你的CircularQueue构造函数是错误的。

将它更改为:

public CircularQueue(int inMax) {
    queue = new Object[inMax];
    int count = 0;
}

你的错误是因为当你有一个带构造函数的类时...
构造函数的名称必须与类名相同。

但是在你的例子中,类名是CircularQueue,而构造函数名是Queue

英文:

dude, Queue is an interface which means if u want to use it (abstraction) to your class u have to use implements keyword

the extends works for extending of a Class... but Queue is an interface
your code is extending Queue and it is wrong

use this:

public class CircularQueue implements Queue

and don't forget to implement all Queue methods or make your class abstract

and one more thing

your constructor of CircularQueue is wrong

change it to this:

public CircularQueue(int inMax)
      {
          queue = new Object[inMax];
          int count = 0;
      }

your error is for this problem that when u have a Class with constructor ...
the constructor MUST be same name with Class name

but in your example Class name is CircularQueue but the constructor name is Queue

huangapple
  • 本文由 发表于 2020年9月6日 22:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63765133.html
匿名

发表评论

匿名网友

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

确定