java - 如何通过组合在另一个对象中编写对象数组以及如何在主类中创建它们?
问题描述
例如,有一个程序,我可以在 Book 对象(构造函数)中编写许多作者。错误只出现在main中,但其他类中可能有一些代码,应该写不同的。
```
package ex1;
public class Author {
private String name, email;
private char gender;
public Author(String name, String email, char gender) {
this.name = name;
this.email = email;
this.gender = gender;
}
public String toString() {
return "Author [name=" + name + ", email=" + email + ", gender=" + gender + "]";
}
public String getName() {
return name;
}
public void setEmail(String email) {
this.email = email;
}
public char getGender() {
return gender;
}
}
```
```
package ex1;
import java.util.Arrays;
public class Book {
private String name;
private Author[] authors;
private Page page;
private double price;
private int qty = 1;
public Book(String name, Author[] authors, double price) {
this.name = name;
this.authors = authors;
this.price = price;
}
public Book(String name, Author[] authors, Page page, double price, int qty) {
this.name = name;
this.authors = authors;
this.price = price;
this.qty = qty;
this.page = page;
}
@Override
public String toString() {
return "Book [name=" + name + ", authors=" + Arrays.toString(authors) + ", page=" + page + ",
price=" + price + ", qty=" + qty + "]";
}
public String getName() {
return name;
}
public Author[] getAuthors() {
return authors;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//....
}
```
课程页面至少可以正常工作。
```
package ex1;
public class Page {
private int pageNumber, numberOfWords;
private String footnote;
public Page(int pageNumber, int numberOfWords, String footnote) {
this.pageNumber = pageNumber;
this.numberOfWords = numberOfWords;
this.footnote = footnote;
}
@Override
public String toString() {
return "Page [pageNumber=" + pageNumber + ", numberOfWords=" + numberOfWords + ", footnote=" +
footnote + "]";
}
public int getPNr() {
return pageNumber;
}
public int getWords() {
return numberOfWords;
}
public String getFoot() {
return footnote;
}
}
```
所以在这里我希望看到我可以像这样或以类似的方式创建一本书:Book b2 = new Book("Ac2", authors[authorAna, Kratos], 35);
```
package ex1;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Author authors[] = new Author[2]; // this is a method but it doesn't work as intended
Author authorAna = new Author("Ana", "A@em.com", 'f');
Author Kratos = new Author("Kratos", "K@em.com", 'm');
authors[0] = authorAna;
authors[1] = Kratos;
Page p6 = new Page(6, 400, "He jumped into the haystack to hide from enemies");
Book b1 = new Book("Ac1", authors, 25);
//Book b2 = new Book("Ac2", authorAna, 35);
//Book b3 = new Book("God of War1", Kratos, 20);
//Book b4 = new Book("GoW2", , p6, 20, 40);
System.out.println(Kratos + "\n" + b1.toString());
//System.out.println(b2);
//System.out.println(b3);
//System.out.println(b4);
}
}
```
解决方案
您可以像这样创建作者数组。然后您需要索引数组以获取单个作者对象。
Author[] authors = {new Author("Ana", "A@em.com", 'f'),
new Author("Kratos", "K@em.com", 'm')};
Book b1 = new Book("Ac1", authors, 25);
如果需要,您可以以相同的方式创建书籍数组或创建书籍构建器。
class Book {
private String name;
private Author[] authors;
private Page page;
private double price;
private int qty = 1;
private Book() {
}
public Book(String name, Author[] authors, double price) {
this.name = name;
this.authors = authors;
this.price = price;
}
public Book(String name, Author[] authors, Page page,
double price, int qty) {
this.name = name;
this.authors = authors;
this.price = price;
this.qty = qty;
this.page = page;
}
@Override
public String toString() {
return "Book [name=" + name + ", authors="
+ Arrays.toString(authors) + ", page=" + page
+ ", price=" + price + ", qty=" + qty + "]";
}
public String getName() {
return name;
}
public Author[] getAuthors() {
return authors;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public static Book bookBuilder() {
return new Book();
}
public Book price(double price) {
setPrice(price);
return this;
}
public Book quantity(int quantity) {
qty = quantity;
return this;
}
public Book name(String name) {
this.name = name;
return this;
}
}
它会像这样使用。
Book b = Book.bookBuilder().name("Ac2").price(40.).quantity(20);
请注意,您可以修改该bookBuilder
方法以接受 book 类中始终需要的任何元素。然后你可以添加你需要的任何方法。构建器的另一个不错的功能是调用方法的顺序无关紧要(当然第一个除外)。
推荐阅读
- javascript - 为什么我的环境变量在反应中未定义?
- android - 如何在 adnroid 中更新 Room 版本?
- python - 为什么我得到不同的 roc_auc_scores?
- javascript - AG Grid 在初始化时展开行
- excel - @ 运算符在不需要时出现
- jsf-2.2 - Mojarra 2.2 中与 Flash 相关的 cookie 问题
- python-3.x - Python:如何为 numpy.int64 打印带有多个参数的 for 循环?
- flutter - CustomPaint 在 Flutter 中设计一个下一个
- r - 在 R 中并行化随机森林学习改变了 RF 对象的类别
- android - 如何为列表视图适配器中的开关添加点击监听器