簡體   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