【问题标题】:Constructor in class cannot be applied to given types. Hope for assistance类中的构造函数不能应用于给定类型。希望得到帮助
【发布时间】:2013-10-07 05:18:07
【问题描述】:

我对@9​​87654321@ 还很陌生,我正在使用BlueJ。我不断收到错误:

constructor ItemNotFound in class ItemNotFound cannot be applied to given types;
required: int
found: no arguments
reason: actual and formal arguments lists differ in length

我很困惑,不知道如何解决这个问题。希望有人可以帮助我。提前谢谢你。

这是我的班级目录:

public class Catalog {
    private Item[] list;
    private int size;

    // Construct an empty catalog with the specified capacity.
    public Catalog(int max) {
        list = new Item[max];
        size = 0;
    }

    // Insert a new item into the catalog.
    // Throw a CatalogFull exception if the catalog is full.
    public void insert(Item obj) throws CatalogFull {
        if (list.length == size) {
            throw new CatalogFull();
        }
        list[size] = obj;
        ++size;
    }

    // Search the catalog for the item whose item number
    // is the parameter id.  Return the matching object 
    // if the search succeeds.  Throw an ItemNotFound
    // exception if the search fails.
    public Item find(int id) throws ItemNotFound {
        for (int pos = 0; pos < size; ++pos){
            if (id == list[pos].getItemNumber()){
                return list[pos];
            }
            else {
                throw new ItemNotFound(); //"new ItemNotFound" is the error
            }
        }
    }
}

作为参考,这里也是class ItemNotFound 的代码:

// This exception is thrown when searching for an item
// that is not in the catalog.
public class ItemNotFound extends Exception {
    public ItemNotFound(int id) {
        super(String.format("Item %d was not found.", id));
    }
}

【问题讨论】:

    标签: java constructor bluej


    【解决方案1】:

    throw new Item() 无效,因为已定义显式构造函数 Item(int id)。

    【讨论】:

    • 请再来。我不明白这与问题有什么关系。
    • 对不起,我的错误,我以为您将 int 参数传递给构造函数: super(String.format("Item %d was not found.", id));它存在于目录类中。实际上, throe new Item() 是无效的,因为您已经定义了一个显式的构造函数 item(int id)...请忽略我之前的回答。
    • 编辑您的答案并将其发布在此处。在 cmets 中没有。
    【解决方案2】:

    你提供了一个没有任何参数的构造函数。

    public ItemNotFound()
    

    您正在调用new ItemNotFound(id),它在创建ItemNotFound 类的实例时需要一个带有one parameter of type int 的构造函数。所以你需要有一个重载的构造函数

    public class ItemNotFound extends Exception {
        public ItemNotFound() {
            super("Sorry !! No item find with that id"); //Now a generic message.
        }
    
        public ItemNotFound(int if) {
            this(); //Since you are not using any int id in this class
        }
    } 
    

    【讨论】:

      【解决方案3】:

      您为您的类 ItemNotFound 提供了一个自定义构造函数,并且在使用它时未传递所需的参数。

      尝试在此处传递所需的参数

        throw new ItemNotFound(id); 
      

      所以你的代码变成了

      public Item find(int id) throws ItemNotFound {
              for (int pos = 0; pos < size; ++pos){
                  if (id == list[pos].getItemNumber()){
                      return list[pos];
                  }
                  else {
                      throw new ItemNotFound(id); //  Now constructor satisfied
                  }
              }
          }
      

      还有

      throw new ItemNotFound();
      

      当你的班级ItemNotFound 像这样时,上面的那一行是正确的

      // This exception is thrown when searching for an item
      // that is not in the catalog.
      public class ItemNotFound extends Exception {
          public ItemNotFound() {
              super("Sorry !! No item find with that id"); //Now a generic message.
          }
      }
      

      【讨论】:

        【解决方案4】:

        ItemNotFound 类只有一个构造函数:一个接受 int 参数的构造函数:

        public ItemNotFound(int id)
        

        你试图在没有任何参数的情况下调用它:

        throw new ItemNotFound();
        

        这是行不通的——你需要为那个参数传递一个参数。我怀疑你只是想要:

        throw new ItemNotFound(id);
        

        (假设find 方法的id 参数是您要查找的ID。)

        此外,我建议您重命名您的异常以包含 Exception 的后缀以遵循 Java 命名约定 - 所以 ItemNotFoundException

        需要更改您的循环 - 如果 first 值没有正确的 ID,目前您将抛出异常,而您可能想要遍历所有这些。所以你的find 方法应该是这样的:

        public Item find(int id) throws ItemNotFoundException {
            for (int pos = 0; pos < size; ++pos){
                if (id == list[pos].getItemNumber()){
                    return list[pos];
                }
            }
            throw new ItemNotFoundException(id);
        }
        

        【讨论】:

          猜你喜欢
          • 2013-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-18
          相关资源
          最近更新 更多