将对象数组传递给构造函数[重复]

Posted

技术标签:

【中文标题】将对象数组传递给构造函数[重复]【英文标题】:Passing an object array to a constructor [duplicate] 【发布时间】:2020-07-27 19:55:06 【问题描述】:

我创建了一个我在构造函数的方法签名中使用的 Author 对象:public Book [String name, Author author1........] 但是,我正在做的分配要求我将作者(实例变量)更改为Author[]。当然,现在我以前的构造函数将不起作用。这是代码

public class Author 
    private String name;
    private String email;
    private char gender;

    public String getName() 
        return name;
    
    public void setEmail(String email)
        this.email = email;
    
    public String getEmail()
        return email;
    
    public char getGender()
        return gender;
    

    public Author(String name, String email, char gender)
        this.name = name;
        this.email = email;
        this.gender = gender;
    

    public String toString()
        return "Author[name = " + this.name + ", email =  " + this.email + ", gender = " + this.gender + "]";
    


public class Book 
    private String name;
    private Author[] author;
    private double price;
    private int quantity;

    // Getters and Setters
    public String getName()
        return name;
    
    public Author[] getAuthor()
        return author;
    
    public void setPrice(double price)
        this.price = price;
    
    public double getPrice()
        return price;
    
    public void setQuantity(int quantity)
        this.quantity = quantity;
    
    public int getQuantity()
        return this.quantity;
    



    // Constructors
    public Book(String name, Author[] author, int quantity)
        this.name = name;
        this.author = author;
        this.price = price;
    

public class BookTest 
    public static void main(String[] args)
        Author author2 = new Author("Ben", "Ben@hgmail.com", 'm');
        Author author3 = new Author("Jennie", "Jennie@hgmail.com", 'f');
        Book theIdiot = new Book("The Idiot", [author2, author3], 44.5, 33);
    

如果我上传的方式不令人满意,对于给您带来的不便,我深表歉意。我还没有学会使用 Stack Overflow。 谢谢!

【问题讨论】:

我设法忽略了 Book Class public Book(String name, Author[] author, double price, int quantity) this.name = name; this.author = 作者; this.price = 价格; this.quantity = 数量; // 输出 public String toString() return "Book[name = " + this.name + ", Author[name = " + author.toString() + "], price = " + this.price + ", quantity = " + this.数量; 这能回答你的问题吗? How do I declare and initialize an array in Java? 【参考方案1】:

内联数组创建的语法是new ArrayType[]elem1, elemn2

所以

Book theIdiot = new Book("The Idiot", new Author[]author2, author3, 44.5, 33);

或者没有内联的版本

Author[] authors = new Author[2];
authors[0] = author2;
authors[1] = author3;
Book theIdiot = new Book("The Idiot", authors, 44.5, 33);

或如评论中提到的“冷冻豌豆的罗迪”

Author[] authors = author2, author3
Book theIdiot = new Book("The Idiot", authors, 44.5, 33);

【讨论】:

非内联版本也可以是:Author[] authors = author2, author3 ;【参考方案2】:

将构造函数从Author[] 更改为Author...。 IDE 强制您将构造函数的顺序设置为将 Author... 放在最后,这意味着您的构造函数将如下所示:

public Book(String name, int quantity, double price, Author... authors)

在调用构造函数时,可以提供单个实例

new Book("The idiot", 123, 12.63, author1);

或者你可以提供多个实例

new Book("The idiot", 123, 12.63, author1, author2, author3);

【讨论】:

听起来提问者正在做一个作业,其中作者明确是一个数组,而不是一个可变参数。 我很确定你仍然可以用这个传递一个数组 如果构造函数接受数组而不是可变参数,则不会。您可以将数组传递给可变参数,但不能将可变参数传递给数组。

以上是关于将对象数组传递给构造函数[重复]的主要内容,如果未能解决你的问题,请参考以下文章

将数组常量传递给枚举构造函数[重复]

使用构造函数将数组传递给对象后,值已更改(在 c++ 中)。我想不通为啥

为啥我必须将道具传递给构造函数和超级[重复]

将数组传递给构造函数会产生大小为 1 的数组? [复制]

如何将数组传递给构造函数或类?

无法从构造函数中将数组值传递给向量