简体   繁体   English

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

[英]Can I put multiple values in object?

I whant an object with multiple values and in one value a list of Strings.我想要一个具有多个值的 object 和一个字符串列表。 for example a cookbook: The recipes are the objects stored in an arraylist.例如食谱:食谱是存储在 arraylist 中的对象。 Every object should have a name, a boolean (isVeggie) and a list of all the ingredients and the associated quantities.每个 object 都应该有一个名称、一个 boolean (isVeggie) 以及所有成分和相关数量的列表。

I thought of other arraylist inside the object, but I can´t access the list on the object.我想到了 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;

Of course you can create arrays or lists as a property inside another object.当然,您可以创建 arrays 或将其列为另一个 object 中的属性。 Consider the following example:考虑以下示例:

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 contains a list of nicknames. Class User包含昵称列表。 You can create a User object and access it's properties like the following:您可以创建一个User object 并访问它的属性,如下所示:

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

tl;dr tl;博士

Define a record .定义一个record Specify List as one of the member fields.List指定为成员字段之一。

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

Populate with List.of .填充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. 
) 

Access the list of ingredients.访问成分列表。

myRecipe.ingredients() 

Details细节

The Answer by Sergey is correct, and should be accepted.谢尔盖的回答是正确的,应该被接受。 I will add a few thoughts.我将添加一些想法。

Naming conventions命名约定

Mind the naming conventions in Java, as they make your code easier to read.请注意 Java 中的命名约定,因为它们使您的代码更易于阅读。 So your class name should start with an uppercase letter.因此,您的 class 名称应以大写字母开头。 And naming in the singular makes more sense in your case.在您的情况下,以单数命名更有意义。 So: Recipe .所以: Recipe

BigDecimal

In real work, never use a floating-point type where accuracy matters.在实际工作中,永远不要使用精度很重要的浮点类型。 Floating-point technology trades away accuracy for speed of execution.浮点技术牺牲了执行速度的准确性

So for money and such, use BigDecimal class rather than double .因此,为了钱等,请使用BigDecimal class 而不是double

Polymorphism: Use more general interface/superclass多态性:使用更通用的接口/超类

Define your ingredients list property as the more general interface List rather than committing to it always being the concrete class ArrayList .将您的成分列表属性定义为更通用的接口List ,而不是始终将其承诺为具体的 class ArrayList

Unmodifiable list不可修改的列表

Populate that ingredients list with an unmodifiable list if you do not intend to allow the calling programmer to make alterations.如果您不打算允许调用程序员进行更改,请使用不可修改的列表填充该成分列表。

Record记录

If the primary purpose of your class is to transparently and immutably carry data, then define the class as a record .如果您的 class 的主要目的是透明且不可变地携带数据,则将 class 定义为记录 Merely declare your member fields.只需声明您的成员字段。 The compiler implicitly creates the constructor, getters, equals & hashCode , and toString .编译器隐式创建构造函数、getter、 equals & hashCodetoString

Your entire class definition becomes this one line:您的整个 class 定义变为这一行:

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

Example例子

Example usage.示例用法。

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

To access the list of ingredients, call the implicitly generated accessor method whose name is the name of the property (no get prefix).要访问成分列表,请调用名称为属性名称的隐式生成的访问器方法(无get前缀)。

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

To make a modifiable copy of the list, pass to constructor of ArrayList .要制作列表的可修改副本,请传递给ArrayList的构造函数。

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

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

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