簡體   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