微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决类CircularQueue中的构造函数CircularQueue不能应用于给定类型

错误

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

我了解这意味着我没有构造函数接受参数(int),但是在我的课堂上,我知道:

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

CircularQueue也是Queue的子类,这是我的代码中的证明:

public class CircularQueue extends Queue

那为什么会弹出这个错误

注意:我也有一个构造函数,它似乎可以正常工作。

解决方法

老兄,队列是一个接口,这意味着如果您要在班级使用它(抽象),则必须使用implements关键字

extends用于扩展类...但是Queue是一个接口 您的代码正在扩展Queue,这是错误的

使用此:

public class CircularQueue implements Queue

不要忘记实现所有Queue方法或使您的类抽象化

还有一件事

您的CircularQueue构造函数错误

将其更改为此:

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

您的错误是针对以下问题的:当您有一个带有构造函数的Class ... 构造函数必须与类名同名

但在您的示例中,类名称为CircularQueue,但构造函数名称为Queue

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。