在java中设置为构造函数参数[重复]。

问题描述 投票:-3回答:1

我有一个类,我需要创建一个新的对象

public class Order {

    private Set<Shop> shop;
    private LocalDateTime placeDate;
    private String status;

    public Order(Set<Shop> shop, LocalDateTime placeDate, String status) {
        this.shop = shop;
        this.placeDate = placeDate;
        this.status = status;
    }
}

我需要创建一个新的对象 但我不知道如何在构造函数中加入 "Set"。

Order order =  new Order(???, LocalDateTime.now(), "status");

如何才能做到这一点?

java set
1个回答
1
投票

首先,你需要创建一个新的Set,然后你就可以把它传递给构造函数。

// Create new set
Set<Shop> shops = new HashSet<>();

// Fill it with values (shop1, shop2 are objects of the Shop class)
shops.add(shop1);
shops.add(shop2);

// Pass it to the constructor
Order order =  new Order(shops, LocalDateTime.now(), "status");
© www.soinside.com 2019 - 2024. All rights reserved.