我有一个类,我需要创建一个新的对象
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");
如何才能做到这一点?
解决方案:
首先,你需要创建一个新的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");
本文来自投稿,不代表运维实战侠立场,如若转载,请注明出处:https://www.shizhanxia.com/259.html