繁体   English   中英

没有找到适合 java 的构造函数

[英]No suitable constructor found for java

当我尝试使用的构造函数只有一个int参数时,我收到错误消息“找不到合适的构造函数”。 当我尝试使用一个int参数在另一个类中创建一个新实例时,它给了我那个错误。 我在做什么不正确导致此错误?

public class dHeap <T extends Comparable <? super T>> implements dHeapInterface<T> {
  
    private int children;
    private T[] array;
    private int size;
  
    public dHeap (int heapSize){
       array = (T[]) new Comparable[heapSize];
       children = 2;
       size = 0;
    }
    public dHeap (int d, int heapSize) { 
       array = (T[]) new Comparable[heapSize];
       children = d;
       size = 0;
    }
...
}

public class MyPriorityQueue<T extends Comparable <? super T>> extends dHeap<T> {
  private dHeap<T> queue;
  private int size;
  
  public MyPriorityQueue(int queueSize)
  {
    queue = new dHeap<T>(queueSize);
    size = 0;
  }
...
}

错误是

no suitable constructor found for dHeap()
constructor dHeap.dHeap(int,int) is not applicable
  (actual and formal argument lists differ in length)
constructor dHeap.dHeap(int) is not applicable
  (actual and formal argument lists differ in length)

在类 dHeap 的代码中,您没有创建任何默认构造函数,但是在 MyPriorityQueue 中,您正在扩展它,并且构造函数调用了它的超级默认构造函数,在这种情况下不存在。

由于MyPriorityQueue扩展了dHeap ,并且dHeap没有无参数构造函数,因此您必须使用super( ... args here ... )调用适当的构造函数。

由于MyPriorityQueuedHeap ,您可能希望删除字段queue ,并替换queue = new dHeap<T>(queueSize); super(queueSize)

您可能还想删除 field size

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM