简体   繁体   English

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

[英]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.例如,我可以在 object (constructor) many Authors 中编写这个程序。 The errors appear only in main, but there may be some code in other classes, which should be written differently.错误只出现在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;
}
}
```

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

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);所以在这里我希望看到我可以像这样或以类似的方式创建一本书: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.然后您需要对数组进行索引以获取个人作者 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.请注意,您可以修改bookBuilder方法以接受图书 class 中始终需要的任何元素。 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).构建器的另一个不错的功能是调用方法的顺序无关紧要(当然第一个除外)。

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

相关问题 如何在main方法中从另一个类创建List对象? - How to create an object of List from another class in main method? 如何在一个类中创建对象并将它们保存在另一个类中的数组中? - How can i creat Objects in one class and save them in array in another class? 如何通过主 Class 从另一个 Class 调用方法? - How to Call a Method From Another Class, Through The Main Class? 如果我创建父类的数组,如何通过数组对象从子类访问方法? - If I create an array of the parental class, how do I access a method from the sub class through the array object? 如何将多个对象分配给另一个类/对象? - How to assign multiple objects to another class/object? 如何用Java编写可靠的Pure Aggregation(组合)游戏对象? - How to write solid Pure Aggregation (composition) Game Objects in Java? 使用组合时如何从组合的 class 到达主 class? - How to reach main class from composed ones when using composition? 如何在主类中初始化另一个类中的构造函数的Array字段? - How to initialize in the main class an Array field from a constructor in another class? 如何创建特定类类型的对象的数组 - How to create an array of objects of a specific class type 如何创建内部类的对象数组? - How to create array of objects of an inner class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM