超级&子类

时间:2013-12-02 19:44:10

标签: java inheritance subclass

  1. 这是我的应用程序类,我有一个对象数组

  2. loanBook是一个超类,loanDocumentry是一个扩展loanBook的子类。这是在应用程序类的顶部声明的

    public static loanBook [] bookArray = new loanDocumetry [5];

  3. 然后在我的应用程序类中,我必须添加新的文档书,所以我使用扫描仪输入,然后使用它们添加新对象

    bookArray[i] = new loanDocumentry(
        title, author, publisher, year, noOfPages, genre);
    

    并且loanBook中的书数增加所以我知道每次运行该方法时它都会创建新书但是当打印出阵列时,它似乎从未将任何这些书添加到数组中,并且我添加的只有一个是最后一个

    申请类:

    public class ApplicationClass {
    
    public static Scanner input = new Scanner(System.in);
    public static loanBook[] bookArray = new loanDocumentry[5];
    
    public static void main(String[] args) {
        addBook();
    }
    
    public static void addBook() {
        input.nextLine();
    
        String title;
        String author;
        String publisher;
        int year;
        int noOfPages;
        String genre;
        String choice;
        int i = 0;
    
    
                System.out.print("\nTITLE of the book: ");
                title = input.nextLine();
                System.out.print("AUTHOR of the book: ");
                author = input.nextLine();
                System.out.print("PUBLISHER of the book: ");
                publisher = input.nextLine();
                System.out.print("YEAR book was published in: ");
                year = input.nextInt();
                System.out.print("NUMBER OF PAGES the book has: ");
                noOfPages = input.nextInt();
                System.out.print("GENRE of the book: ");
                input.nextLine();
                genre = input.nextLine();
                bookArray[i] = new loanDocuemntry(title, author, publisher, year, noOfPages, genre);
                i++;
    

    }

    loanBook Superclass& loanDocumentry子类都使用set和gets

1 个答案:

答案 0 :(得分:0)

尝试从i上方的行bookArray[i] = new loanDocumenry(开始打印 它应该在每个新的loanBook被添加时改变。

当您添加loanBook时,我应该增加1,因此您要设置bookArray[0]bookArray[1]bookArray[2]bookArray[3]bookArray[4]。你只是设置了第一个。