【问题标题】:Java: Constructor X in class X cannot be applied to given typesJava:类 X 中的构造函数 X 不能应用于给定类型
【发布时间】:2013-07-25 13:26:16
【问题描述】:

我正在创建一个包含类 Library{} 和 Book{} 的包 mylib。

库类

package mylib;
import java.util.*;

class Library {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    Book[] MyBooks = new Book[3];
    Book x;

    MyBooks[0] = new Book("The Lover's Dictionary", "Levithan, D.", 211, "AVAILABLE");
    MyBooks[1] = new Book("White Tiger", "Adiga, A.", 304, "AVAILABLE");
    MyBooks[2] = new Book("Thirteen R3asons Why", "Asher, J.", 336, "AVAILABLE");

    System.out.println("\n***** BOOK LISTING *****");
    for(int i = 0; i < MyBooks.length; i++) {
        x = MyBooks[i];
        System.out.println("[" + (i + 1) + "] " + x.sTitle + "\nAuthor: " +
            x.sAuthor + "\nPages: " + x.iPages + "\nStatus: " + Book.AVAILABLE);
        System.out.print("\r\n");
    }

    System.out.println("***** SELECT ACTION *****");
    System.out.println("B - Borrow a book");
    System.out.println("R - Reserve a book");
    System.out.println("I - Return a book");
    System.out.println("X - Exit program");

    System.out.print("\nEnter command: ");
    char cAction = input.nextLine().charAt(0); // Read single char

    switch(cAction) {
        case 'B':

            break;

        case 'R':

            break;

        case 'I':

            break;

        case 'X':
            Book book = new Book();
            book.exitProgram();
            break;

        default:
            System.out.println("INVALID INPUT!");
            break;
    }
}
}

图书课

package mylib;

class Book {
int iPages;
String sTitle, sAuthor;
String sBorrowedBy, sReservedBy;
String sDueDate, sReturnDate;
    String sStatus;

public static final String BORROWED = "BORROWED", AVAILABLE = "AVAILABLE", RESERVED = "RESERVED";

// Constructor
public Book(String title, String author, int pages, String status) {
    this.sTitle = title;
    this.sAuthor = author;
    this.iPages = pages;
    this.sStatus = status;
}
/*
void borrowBook() {

}

void reserveBook() {

}

 void returnBook() {

}
*/
 void exitProgram() {
    System.exit(0);
}
}

Library 类中,我试图通过switch 退出程序,它从Book 类调用exitProgram() 方法。我收到 1 个错误 constructor x in class x...

任何帮助将不胜感激。

【问题讨论】:

    标签: java constructor package


    【解决方案1】:

    在 Book.java 中将退出方法设为静态

    Book.java
    static void exitProgram() {
      System.exit(0);
    }
    
    Library.java
     case 'X':
            //Book book = new Book();
            Book.exitProgram();
            break;
    

    【讨论】:

    • pastebin.com/3Hch0Qje - 当我输入 X 时它编译但仍然不退出?
    • 它在我的情况下工作......只需尝试如下放置一个 SOP :static void exitProgram() { System.out.println("Gonna exit"); System.exit(0); }
    • cmd-prompt 是否应该终止 Sir?
    • 是...控件应该终止
    • This 是迄今为止发生的事情。这是预期的输出吗? cmd-prompt 仍然是打开的,先生。
    【解决方案2】:
    case 'X':
                Book book = new Book();
                book.exitProgram();
                break;
    

    你没有可以接受无参数的构造函数

    public Book() {
    }
    

    如果你想创建没有参数的“书”,这应该在书类中定义。

    您应该通过传递所需的参数来创建 Book 对象

    case 'X':
                    Book book = new Book("test","test",1,"test");
                    book.exitProgram();
                    break;
    

    【讨论】:

    • 我创建了不接受任何参数的构造函数。它现在编译。但是,输入“X”不会退出程序?
    • 肯定会的。 System.exit() 将杀死 Jvm。如果 JVM 没有运行,程序就无法运行。
    • 我在本地测试过,它正在退出。所以只要检查你在做什么
    • 嗨 Reddy,这是我目前所做的 - pastebin.com/3Hch0Qje - 哪里出了问题?
    • 带有构造函数更改的初始代码在我的系统中运行良好。我现在测试了,它肯定会工作。没有理由,它不会工作。你用的是哪个JDK?
    【解决方案3】:

    仅当类中没有定义构造函数时,Java 运行时才提供默认的无参数构造函数。在您的情况下,您有一个带有少量参数的构造函数,因此 Java 运行时不会添加无参数构造函数。您实际上必须在 Book 类中放置一个无参数构造函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多