【问题标题】:Java how can I make a book class that can have one or more authorsJava如何制作一个可以有一个或多个作者的书类
【发布时间】:2019-05-03 21:43:27
【问题描述】:

所以我正在练习面向对象的编程,我正在尝试制作一个可以有多个作者的 Book 类,但我不知道该怎么做。 这是练习的UML:

这是我的作者类,效果很好:

public class Author {

    //attributen van de class auteur
    private String name;
    private String email;
    private char gender;

    //constructor
    public Author (String name, String email, char gender){
        this.name = name;
        this.email = email;
        this.gender= gender;
    }

    //methodes
    public String getName(){
        return name;
    }

    public String getEmail(){
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }

    public char getGender(){
        return gender;
    }

    //methode om gegevens van autheur opbject op te halen
    public String toString(){
        return "Author[name = " + name + ", email = " + email + ", gender = " + gender + "]";
    }
}

这是我尝试制作的 Book 类:

public class Book {

    //attributes
    private String name;
    private Author authors [] = new Author[2];
    private double price;
    private int qty = 0;


    public Book(String name, Author authors[], double price, int qty) {
        this.name = name;
        authors[0] = new Author("Tan Ah Teck", "AhTeck@somewhere.com", 'm');
        authors[1] = new Author("Paul Tan", "Paul@nowhere.com", 'm');
        this.price = price;
        this.qty = qty;
    }


    public String getName() {
        return name;
    }

    public Author getAuthors() {
        return authors[authors.length];

    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }

    public String toString() {
        return "Book [name = " + name + " authors = " + authors[0] + " email = " + authors[0].getEmail() + " price = " + price + " qty = " + qty + "]";
    }

    //methodes om gegevens van de autheur op te halen
    public String getAuthorNames() {
        return authors[].getName();
}

    public String getAuthorEmails() {
        return authors[].getEmail();
    }

    public char getAuthorGenders() {
        return authors[].getGender();
    }
}

因此,当我尝试在 main.java 中创建书的对象时,书类的构造函数不起作用。 也在这个功能:public Author getAuthors() { 它说:数组索引超出范围。 同样在获取作者姓名、电子邮件和性别的方法中,它说:Unknown class authors[]。

如何修改此图书类别,以便一本书可以有一个或多个作者? (当一本书只能有 1 个作者时,Book 类确实有效,但现在我正在尝试更改它,以便一本书可以有更多作者)

感谢任何形式的帮助!

【问题讨论】:

  • 看看 ArrayList。
  • 你的主要方法在哪里,你在哪里执行应用程序。请提供代码!
  • 您可以使用ArrayList<Author>,它可以让您拥有一本书的动态作者数量。
  • 首先尝试使用List docs.oracle.com/javase/8/docs/api/java/util/List.html 而不是数组。
  • 这段代码无法编译。我看不出您如何运行它并获得运行时异常。使用定义数组的标准方法:private Author[] authors = new Author[2];。变量nameauthors*. The variable *type* is Àauthor[]. It's thus an array if Authors, and arrays don't have any getName()`方法。您的最后三个方法没有意义:返回多个作者姓名的方法如何返回单个字符串?

标签: java arrays class oop object


【解决方案1】:

它说:数组索引超出范围

首先,它不能用给定的代码这么说,因为它不能用最后三个方法编译。

好吧,authors.length == 2,你的数组只有索引 0 和 1

如果你想真正“获取作者”,你想返回数组,而不是一个特定的

public Author[] getAuthors() {
    return authors;
}

如果您确实想获得一个,请添加索引。

添加边界检查也是一个好习惯

public Author getAuthor(int index) {
    return (index < 0 || index >= authors.length) ? null : authors[index];
}

但是,通过这些修复,您现在可能会看到 null,因为您实际上有 两个 列表;您将传递给new Book() 的那个,以及该对象实例中的字段。

相反,你会想要做

private Authors[] authors;

public Book(String name, Author authors[], double price, int qty) {
    this.name = name;
    this.authors = authors;

并且在main方法中创建作者

Author[] authors = new Author[] {
    new Author("Tan Ah Teck", "AhTeck@somewhere.com", 'm'),
    new Author("Paul Tan", "Paul@nowhere.com", 'm')
};
Book b = new Book("name", authors, ...);
System.out.println(Arrays.toString(b.getAuthors()));

在获取作者姓名、电子邮件和性别的方法中,它说:Unknown class authors[]

因为authors[].blah 不是有效的语法。您需要遍历每个数组值以获取相应的字段。

我会建议 Arrays.stream 方法和 StringJoiner 来处理这个

【讨论】:

  • 你好,谢谢它的工作,但现在我想制作一个关于如何调用作者姓名的函数,有没有关于如何使用 array.stream 和 stringjoiner 的示例?
  • 现在坚持使用数组和 for 循环,并等待流,直到您了解了基础知识。
【解决方案2】:

如果您没有在构造函数中为作者提供 this 关键字,代码将使用您的构造函数参数而不是您的类变量,因此您会遇到这个问题。

尝试像这样从构造函数中删除作者:

public Book(String name, double price, int qty)

【讨论】:

  • 数组引用被修改了,所以只是参数没有改变...
  • @cricket_007 他理解这个概念有问题,这样他就可以弄清楚我们发生了什么,我相信他之后能够解决他的问题
  • 您可以像其他字段一样只使用this.authors = authors;,然后从字段中删除new Author[2]
  • @cricket_007 这不是一个好的解决方案,因为他可能会传递一个长度为 1 的数组,并且在他的构造函数中添加两个元素后,会发生 IndexOutOfBoundException !
  • @cricket_007 当然,如果他删除那些作者[0]和作者[1],那就没问题了!你在开玩笑吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 2011-12-16
  • 2023-03-05
相关资源
最近更新 更多