首页 文章

无法通过Java中的构造函数初始化对象:错误编译

提问于
浏览
0

下面的代码不断引发以下错误:Java Error Terminal Screen Snapshot

我已经尝试了很多在线修复,但它们似乎没有工作 . 我的代码在下面,我已经评论了错误位置(发生在驱动程序主函数中)注意:如果我将public static void main(String args [])更改为public void main(String args []),代码将正确编译但是当我运行它时,它会抛出错误“将main更改为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();
    }
}

} //练习结束

1 回答

  • 0

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

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

相关问题