简体   繁体   中英

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

For example, there is this program where I could write in the Book object (constructor) many Authors. The errors appear only in main, but there may be some code in other classes, which should be written differently.

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

In the photo, you can see the program requirements.

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

//....
}
```

The class page is working at least.

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

So here I would like to see that I could create a Book like this or in a similar manner: 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);
}
}
```

You can create the Author array like this. Then you would need to index the array to get the individual author object.

    Author[] authors = {new Author("Ana", "A@em.com", 'f'),
                 new Author("Kratos", "K@em.com", 'm')};
    
    Book b1 = new Book("Ac1", authors, 25);

If need be, you could then create a book array the same way or create a book builder.

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

It would be used like this.

Book b = Book.bookBuilder().name("Ac2").price(40.).quantity(20);

Note that you can modify the bookBuilder method to accept any elements of the book class that would always be required. Then you can add whatever methods you need. And the other nice feature of builders is that the order you call the methods doesn't matter (except for the first one of course).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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