当前位置: 首页 > 知识库问答 >
问题:

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

夔学智
2023-03-14

我创建了一个Author对象,用于构造函数的方法签名:public Book[String name,Author Author1.......]但是,我所做的赋值要求将Author(实例变量)更改为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);
    }
}

如果我上传的方式不令人满意,我对任何不便表示歉意。我还没有学会使用堆栈溢出。
谢谢!

共有1个答案

欧阳炜
2023-03-14

内联数组创建的语法为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);
 类似资料:
  • 我试图使一个用户类和使用类来处理登录,但从某种原因我的用户类构造函数是读取我的mysqli对象作为一个字符串 这是我的密码 Class.php mysql.php checklogin.php 它给了我这个错误 可捕获的致命错误:在第13行的C:\wamp\www\class.php中,类mysqli的对象无法转换为字符串 我要把头发拔出来了,有什么想法吗?

  • Java枚举允许您将参数传递给构造函数,但我似乎不能传递数组。例如,下面的代码编译时有错误: 但如果将相同的数据作为数组常量传入,则代码将无法编译: 我也尝试过创建新的int[]数组的变体,比如: 没有运气。我认为问题在于传递一个数组常量。我不确定这是一个需要修正的简单语法,还是在传递这种类型的数据时存在一个潜在的问题。提前道谢。

  • 我试图将构造函数参数动态传递给Springboot框架中的一个bean。我已经使用context.getBean(class,arg...)在Spring中动态传递构造函数参数,但它没有成功获取值并显示默认值。我的代码有什么问题? 项目结构: 应用程序上下文.xml 应用 活动 输出:

  • 问题内容: 在处理中,我定义了以下类: 现在,我想创建该类的实例,但是我很难将数组传递给构造函数: 但这总是给我一个错误“意外令牌:{”。因此,显然无法“即时”定义数组。 但是,这将起作用: 但是我发现在对象外部声明该数组是很愚蠢的,因为这会在创建多个对象时带来各种麻烦: 现在,该类的两个实例都引用了相同的数组,这当然不是我想要的。 所以我的问题是:如何在不事先声明数组的情况下将数组正确传递给类的

  • 我为什么要这样做?因为lambda表达式生成的ClosureType不是默认可构造的。通过这个“技巧”,我可以默认构造这样的closureType。 此外,模板参数的要求是,它必须为空=>

  • 问题内容: 大家晚上好。 当尝试将一个对象传递给另一个对象构造函数时,我似乎遇到了一个奇怪的问题,该对象的构造函数也依赖于要传递给它的对象。 例如,使用以下示例: 但是,在构造ToolBar时,它将返回NullPointerException。当然,这是因为尚未构建webPanel并且需要它。 减速和初始化都必须保留在同一类(称为BuildUI)中,因为这是我设置属性的地方。(对于ToolBar创