如何从 start() 函数打印此对象

How to print this objects from start() function

我创建了 3 classes:书籍、作者和主要 class。我在其中创建了 start() 方法和对象(任务需要)。这是我的鳕鱼,有人可以解释一下吗,或者有更好的方法吗? 我创建数组列表,有更好的方法吗?我非常感谢任何帮助

import java.util.ArrayList;
import java.util.Scanner;

public class BookAuthorStorage<E> {
    ArrayList<Book> bookStorage = new ArrayList <>();
    ArrayList<Author> authorsStorage = new ArrayList<>();
    public void start() {
        Book Alchemist = new Book("Alchemist", "That everything is possible", 100, "Paulo Coelho");
    Book HarryPotter = new Book("Harry Potter", "The Kind take over evil", 300, "J.K. Rowling");
    Book It = new Book("It", "Scary book", 200, "Stephen King");
    Author StephenKing = new Author("Stephen", "King", 46, "male", "King@mail.com", "It");
    Author Rowling = new Author("Joan", "Rowling", 39, "female", "Rowling.com", "Harry Potter");
    Author Coelho = new Author("Paulo", "Coelho", 60, "male", "Coelho.com", "Alchemist");

        Scanner scan = new Scanner(System.in);
        System.out.println(" Enter 1 to show all books or 2 for authors and 3 for exit");
        while (true) {
            int choice = scan.nextInt();
            switch (choice) {
                case 1 -> bookStorage.forEach(System.out::println);
                case 2 -> authorsStorage.forEach(System.out::println);
                case 3 -> {
                    System.out.println(" Exiting");
                    return;
                }
                default -> System.out.println("Enter again");
            }
        }
    }

    public static void main(String[] args) {
       BookAuthorStorage bookAuthorStorage1 = new BookAuthorStorage();
        bookAuthorStorage1.start();


    }

}

当我尝试打印时,它打印出 null、null、null。我覆盖了 Book and Author classes

中的字符串
 public class Author {
        public  String name;
        public  String surname;
        public  int age;
        public  String gender;
        public  String email;
    
    Author(String name, String surname, int age, String gender,String email, String book){

}
       public String toString(){
          return this.name  + this.surname  + this.age + this.email + this.gender ;
       }
    
    
    }




    public class Book {
        public String title;
        public String description;
        public int count = 1000;
        public String author;
Book(String title, String description, int count, String author){

}
    
        @Override
       public String toString(){
            return this.author + this.description + this.title + this.count;
        }
    }

使用所有成员变量为 BookAuthor 创建参数化构造函数,然后在 BookAuthorStorage 中初始化两个 类,例如:

    Author(String name, String surname, int age, String gender,String email, String book){
  this.name=name;
  this.surname=surname;
  this.age=age;
  this.gender=gender;
  this.email=email;
  this.book=book;
}
   

并在 BookAuthorStorage 内调用:

    Book Alchemist = new Book("A","B",1,"C");
    ..
    Author StephenKing = new Author("D","E",1,"F","G");
    ..

目前在原始代码中,除了 int 成员变量之外,您没有初始化任何 String 成员变量。所以这就是你的输出总是有 null & int 值的原因。

在构造函数中,参数和内部变量之间没有隐式关系。
尽管名称相同,但您必须指明要对参数执行的操作:

Book(String title, String description, int count, String author){
this.title = title; 
this.description = description;
this.count = count;
this.author = author;
} 

最好区分两者以避免错误,因为参数名称可以在构造函数中使用。

Book(String aTitle, String aDescription, int aCount, String anAuthor){
this.title = aTitle; 
this.description = aDescription;
this.count = aCount;
this.author = anAuthor;
}