繁体   English   中英

如何通过组合在另一个 object 中写入对象数组以及如何在主 class 中创建它们?

[英]How to write an array of objects through composition in another object and how to create them in main class?

例如,我可以在 object (constructor) many Authors 中编写这个程序。 错误只出现在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;
}

//....
}
```

class 页面至少可以正常工作。

```
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);
}
}
```

您可以像这样创建作者数组。 然后您需要对数组进行索引以获取个人作者 object。

    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方法以接受图书 class 中始终需要的任何元素。 然后你可以添加你需要的任何方法。 构建器的另一个不错的功能是调用方法的顺序无关紧要(当然第一个除外)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM