来自另一个类的Field的构造函数未定义?

时间:2015-01-03 03:35:58

标签: java class object

我的新任Java教程的任务是执行以下操作

  1. 在用户类中:
    1. 创建一个名为readBook(Book book)的方法
    2. readBook应该打印,&#34; <User> read <title>&#34;
  2. 在BookRunner中
    1. 为每本书创作一个作者。
    2. 另外,创建另一个用户并在该用户上调用readBook方法,传入一个已创建的书籍
  3. 以下是我的代码:

    public class Ex1_BookRunner {
        public static void main(String[] args) {
            Book firstBook = new Book("One Piece", "Oda-Sensei", 100, 123456);
            Book secondBook = new Book("Life of Megan Fox", "Micheal Bay", 200, 928765);
        }
    }
    
    public class User {
        public String name;
        public int age;
        public String location;
        public Book title;
    
        public String toString() {
            String description1 = "Name:" + name + " Age:"+ age + " Location:" + location; 
            return description1;
        }
    
        public void readBook(Book book) {
            System.out.println(name + " read " + title.title);
        }
    }
    
    public class Book {
        public String title;
        public User author;
        public int numPages;
        public int isbn;
    
        public Book(String title, User author, int numPages, int isbn) {
            this.title = title;
            this.author =  author;
            this.numPages = numPages;
            this.isbn = isbn;
        }
    
        public String toString() {
            String description = "Title:" + title + "Author"+ author.name + "Num. of pages" + numPages + "ISBN" + isbn; 
            return description;
        }   
    }
    

    我甚至无法理解问题要我做什么。我有以下问题

    1. 让我用readBook方法打印<User> read <title>是什么意思?我是否按照设置readBook方法的方式正确解释了问题?

      方法中的正确代码应该是什么?

    2. 我在创建author时遇到问题,因为我收到错误The constructor Book(String, String, int, int) is undefined并且Eclipse IDE要我从Book更改(String, User, int, int)中的构造函数到(String, String, int, int)。用户实验。&#34; Oda-Sensei&#34;,&#34; Oda-Sensei&#34; .name,&#34; Oda-Sensei.User&#34;所有这些都让我注意到变量I引入了cannot be resolved

1 个答案:

答案 0 :(得分:1)

从提问中,听起来像作者是用户。你在main方法中的构造函数调用是传入字符串,因此你的构造函数错误。如果作者是用户,那么您应该从以下地址更改您的电话:

public static void main(String[] args) {
    Book firstBook = new Book("One Piece", "Oda-Sensei", 100, 123456);
    Book secondBook = new Book("Life of Megan Fox", "Micheal Bay", 200, 928765);
}

public static void main(String[] args) {
    User odaSensei = new User();
    odaSensei.name = "Oda-Sensei";

    User michaelBay = new User();
    michaelBay.name = "Michael Bay";


    Book firstBook = new Book("One Piece", odaSensei, 100, 123456);
    Book secondBook = new Book("Life of Megan Fox", michaelBay, 200, 928765);
}

确保设置作者的其他变量另一个选项是将作者的类型更改为String,在这种情况下,main方法不会有更改。

我还建议为用户添加构造函数,这样就可以初始化新调用中的所有变量。

第二部分“另外,创建另一个用户并在该用户上调用readBook方法,传入一个已创建的书籍”

告诉您创建一个全新的用户,例如鲍勃

User bobUser = new User();
bobUser.name="Bob"; 

然后带上这个新用户并让他阅读其中一本书:

bobUser.readBook(firstBook); 

目标是让阅读的书籍打印出来:

Bob read One Piece

为此,您需要更改readBook方法:

public void readBook(Book book) {
    System.out.println(name + " read " + title.title);
}

到:

public void readBook(Book book) {
    System.out.println(name + " read " + book.title);
}