【问题标题】:Unable to initialize an object through constructors in Java: Erroneous Compilation无法通过 Java 中的构造函数初始化对象:错误编译
【发布时间】:2018-10-20 15:08:58
【问题描述】:

以下代码不断抛出以下错误: Java Error Terminal Screen Snapshot

我已经尝试了很多在线修复,但它们似乎都不起作用。 我的代码在下面,我已经注释了错误位置(发生在驱动程序主函数中)

注意:如果我将 public static void main(String args[]) 更改为 public void main(String args[]) 但当我运行时,代码会正确编译它会抛出错误“Change main to static void main”。我有点卡在这里。

import java.util.*;
class Exercise{ //Exercise Begins
    class UtilityFunctions{
        public String getAuthor(){ return "";};
        public String  getPublisher(){return "";};
        public void display(){};
    }

class Book extends UtilityFunctions{
    private String title;
    private String author;
    private String category;
    private String datePublished;
    private String publisher;
    private double price;

    public Book(String authorParam, String publisherParam){
        author = authorParam;
        publisher = publisherParam;
    }
    //List of Setters
    public void setTitle(String title){
        this.title = title;
    }
    public void setCategory(String cat){
        this.category = cat;
    }
    public void setDatePublished(String dp){
        this.datePublished = dp;
    }
    public void setPrice(double p){
        this.price = p;
    }

    //List of Getters
    public String getTitle(){
        return this.title;
    }
    @Override
    public String getAuthor(){
        return this.author;
    }
    public String getCategory(){
        return this.category;
    }
    public String getDatePublished(){
        return this.datePublished;
    }
    @Override
    public String getPublisher(){
        return this.publisher;
    }
    public double getPrice(){
        return this.price;
    }
    @Override
    public void display(){
        System.out.println("Book Title:" + getTitle());
        System.out.println("Author:" + getAuthor());
        System.out.println("Category:" + getCategory());
        System.out.println("Date Published:" + getDatePublished());
        System.out.println("Publisher:" + getPublisher());
        System.out.println("Price:$" + getPrice());
    }
}

class Author extends UtilityFunctions{
    private String authorName;
    private String birthDate;
    private String publisher;
    private String email;
    private String gender;
    List<Book> bookList;

    public Author(String publisherParam){
        publisher = publisherParam;
    }

    public void addBook(Book b){
        bookList.add(b);
    }
    //List of Setters
    public void setName(String n){
        this.authorName = n;
    }
    public void setEmail(String em){
        this.email = em;
    }
    public void setGender(String gen){
        this.gender = gen;
    }
    public void setBirthDate(String dob){
        this.birthDate = dob;
    }
    //List of Getters
    public String getAuthor(){
        return this.authorName;
    }
    public String getPublisher(){
        return this.publisher;
    }
    public String getEmail(){
        return this.email;
    }
    public String getGender(){
        return this.gender;
    }
    public String getBirthDate(){
        return this.birthDate;
    }

    @Override
    public void display(){
        System.out.println("Author Name:" + getAuthor());
        System.out.println("Email:" + getEmail());
        System.out.println("Gender:" + getGender());
        System.out.println("BirthDate:" + getBirthDate());
        System.out.println("Publisher:" + getPublisher());

        System.out.println("BOOKS:");
        for(Book b:bookList){
            b.display();
            System.out.println();
        }
    }   

}

class Publisher extends UtilityFunctions{
    private String publisherName;
    private String publisherAddress;
    private String publisherEmail;
    private int publisherPhoneNumber;
    List<Author> authorList;

    public Publisher(String name, String add, String email,int phone){
        publisherName = name;
        publisherAddress = add;
        publisherEmail = email;
        publisherPhoneNumber = phone;
    }
    public void addAuthor(Author a){
        authorList.add(a);
    }
    //List of Getters
    public String getPublisher(){
        return this.publisherName;
    }
    public String getAddress(){
        return this.publisherAddress;
    }
    public String getEmail(){
        return this.publisherEmail;
    }
    public int getPhoneNumber(){
        return this.publisherPhoneNumber;
    }
    @Override
    public void display(){
        System.out.println("Publisher Name:" + getPublisher());
        System.out.println("Publisher Address:" + getAddress());
        System.out.println("Publisher Phone Number:" + getPhoneNumber());
        System.out.println("Publisher Email:" + getEmail());

        System.out.println("AUTHORS:");
        for(Author a:authorList){
            a.display();
            System.out.println("-------------------------------------");
        }

        System.out.println("--------------------------------------------------------------------------------");
        System.out.println("--------------------------------------------------------------------------------");

    }

}

public static void main(String args[]){
    ArrayList<Publisher> publisherList = new ArrayList<Publisher>();
    //1st ERROR HERE
    Publisher pub1 = new Publisher("Riverhead Books","8080 Cross Street","riverhead@riverheadbooks.co.uk",784646533);
    //2nd ERROR HERE
    Author author1 = new Author(pub1.getPublisher());
    author1.setName("Khaled Hosseini");
    author1.setGender("Male");
    author1.setBirthDate("1965-10-09");
    author1.setEmail("khaledhosseini@gmail.com");
    pub1.addAuthor(author1);

    //3rd ERROR HERE
    Book book1 = new Book(author1.getAuthor(),author1.getPublisher());
    book1.setTitle("Kite Runner");
    book1.setCategory("Historical-Fiction|Drama");
    book1.setPrice(39.95);
    book1.setDatePublished("2003-05-29");
    author1.addBook(book1);

    publisherList.add(pub1);
    for(Publisher p:publisherList){
        p.display();
    }
}

}//练习结束

【问题讨论】:

  • 使每个类成为顶级类,在其自己的单独文件中定义。您正在使用内部类,但您还不了解何时以及如何使用它们(这里没有充分的理由)。
  • 此外,您的 UtilityFunctions 类没有什么意义,而且 Author 类确实没有理由拥有方法 getAuthor()(例如)。您最好放弃该类,并了解有关继承的更多信息。
  • 是的,我确实想过很多次放弃 UtilityFunctions 类。但是,作者代表一个实体,所以它应该有自己的类对吗?我的目标是为每个出版商、他们的所有作者以及每个作者的所有书籍打印出来。
  • 是的,它应该有自己的类。你应该放弃的类是 UtilityFunctions,而不是 Author。
  • @JBNizet 感谢关于使每个类都成为顶级类以及 UtilityFunctions 类的建议。我会合并它们。

标签: java constructor initialization


【解决方案1】:

您的所有课程:Book、Author、Publisher、UtilityFunctions ... 应该在 Exercise 课程之外

Class Exercise {
  public static void main(String args[]) {...}
} // end class exercice

Class UtilityFunctions {...}
Class Book {...}
Class Author {...}
Class Publisher  {...}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2017-08-15
    • 2013-10-29
    • 2012-03-05
    • 2012-09-15
    相关资源
    最近更新 更多