繁体   English   中英

我可以在 object 中输入多个值吗?

[英]Can I put multiple values in object?

我想要一个具有多个值的 object 和一个字符串列表。 例如食谱:食谱是存储在 arraylist 中的对象。 每个 object 都应该有一个名称、一个 boolean (isVeggie) 以及所有成分和相关数量的列表。

我想到了 object 中的其他 arraylist,但我无法访问 object 上的列表。

public String name;
public double cost;
public boolean isClassic;
public boolean isVeggie;
public boolean isVegan;
protected ArrayList<String> ingredients  = new ArrayList<String>();

    
public recipes (String name, double cost, boolean isClassic, boolean isVeggie, boolean isVegan,
        ArrayList<String> ingredients) {
    super();
    this.name = name;
    this.cost = cost;
    this.isClassic = isClassic;
    this.isVeggie = isVeggie;
    this.isVegan = isVegan;
    this.ingredients = ingredients;

当然,您可以创建 arrays 或将其列为另一个 object 中的属性。 考虑以下示例:

public class User {
    private String name;
    private List<String> nicknames;

    public User(String name, List<String> nicknames) {
        this.name = name;
        this.nicknames = nicknames;
    }

    public String getName() {
        return name;
    }

    public List<String> getNicknames() {
        return nicknames;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNickNames(List<String> nicknames) {
        this.nicknames = nicknames;
    }
}

Class User包含昵称列表。 您可以创建一个User object 并访问它的属性,如下所示:

User user = new User("RealName", Arrays.asList("nickname1", "nickname2"));
List<String> userNicknames = userr.getNicknames();

tl;博士

定义一个record List指定为成员字段之一。

public record Recipe ( String name, double cost, boolean isClassic, boolean isVeggie, boolean isVegan, List< String > ingredients ) {}

填充List.of

new Recipe(                     // Defined as `public record Recipe`. 
    "Beans & Rice", 
    0.99d, 
    true , 
    true, 
    true, 
    List.of( "beans", "rice" )  // `List.of` produces an object of type `List` using an unspecified concrete class. 
) 

访问成分列表。

myRecipe.ingredients() 

细节

谢尔盖的回答是正确的,应该被接受。 我将添加一些想法。

命名约定

请注意 Java 中的命名约定,因为它们使您的代码更易于阅读。 因此,您的 class 名称应以大写字母开头。 在您的情况下,以单数命名更有意义。 所以: Recipe

BigDecimal

在实际工作中,永远不要使用精度很重要的浮点类型。 浮点技术牺牲了执行速度的准确性

因此,为了钱等,请使用BigDecimal class 而不是double

多态性:使用更通用的接口/超类

将您的成分列表属性定义为更通用的接口List ,而不是始终将其承诺为具体的 class ArrayList

不可修改的列表

如果您不打算允许调用程序员进行更改,请使用不可修改的列表填充该成分列表。

记录

如果您的 class 的主要目的是透明且不可变地携带数据,则将 class 定义为记录 只需声明您的成员字段。 编译器隐式创建构造函数、getter、 equals & hashCodetoString

您的整个 class 定义变为这一行:

public record Recipe ( 
    String name, 
    BigDecimal cost, 
    boolean isClassic,  
    boolean isVeggie, 
    boolean isVegan, 
    List<String> ingredients 
) {}

例子

示例用法。

Recipe beansAndRice = new Recipe( "Beans & Rice", new BigDecimal( "0.99" ), true , true, true, List.of( "beans", "rice" ) ) ;

要访问成分列表,请调用名称为属性名称的隐式生成的访问器方法(无get前缀)。

List< String > ingredients = beansAndRice.ingredients() ;

要制作列表的可修改副本,请传递给ArrayList的构造函数。

List< String > ingredientsToModify = new ArrayList<>( ingredients ) ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM