【问题标题】: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?

标签: 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);

【讨论】:

  • 听起来提问者正在做一个作业,其中作者明确是一个数组,而不是一个可变参数。
  • 我很确定你仍然可以用这个传递一个数组
  • 如果构造函数接受数组而不是可变参数,则不会。您可以将数组传递给可变参数,但不能将可变参数传递给数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 2018-08-04
  • 2014-06-04
  • 1970-01-01
  • 2012-07-09
  • 2011-12-22
  • 2023-03-30
相关资源
最近更新 更多