简体   繁体   English

我怎样才能 model 一个 class 具有另一个 class 的特定实例计数?

[英]How can I model one class having a instance-specific count of instances of another class?

If I have two different classes (say, person class and commodity class), how do I represent each person's individual demand (a number) for each good?如果我有两个不同的类别(例如,人 class 和商品类别),我如何表示每个人对每种商品的个人需求(一个数字)?

If I initialize say 10 instances of the person class, and then 5 different types of good: each person has their own personal demand for each type of good, but if I only initialize the good class as a separate class (so we'll have 5 instances of the good class in this example), how do I give each instance of the person their own specific demand for each of the 5 good instances?如果我初始化 class 的 10 个实例,然后是 5 种不同类型的商品:每个人对每种商品都有自己的个人需求,但如果我只将商品 class 初始化为单独的 class(所以我们将有在这个例子中是 class 的 5 个实例),我如何为每个人的每个实例提供他们对 5 个好的实例中的每一个的特定需求?

I don't really know how to ask this in clearer language and hope I was specific enough that you know what I'm trying to ask.我真的不知道如何用更清晰的语言来问这个问题,希望我足够具体,让你知道我想问什么。 Please reply if you have questions!有问题请回复!

I can think of a couple of simple ways to implement this;我可以想到几个简单的方法来实现它; which one is more appropriate might depend on how you need to use a Person 's commodity demands.哪个更合适可能取决于您需要如何使用Person的商品需求。

The first idea is for each Person to hold a Map of Commodity to count, to indicate which commodities, and how much of of each, that person demands.第一个想法是让每个Person持有MapCommodity进行计数,以表明该人需要哪些商品以及每种商品的数量。 Something like this:是这样的:

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@EqualsAndHashCode
@AllArgsConstructor
public class Commodity {

    @Getter
    private UUID id;

    @Getter
    private String name;
}
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class Person {

    private Map<Commodity, Integer> demands = new HashMap<>();

    public void setDemand(Commodity commodity, Integer count) {
        demands.put(commodity, count);
    }

    public Optional<Integer> getDemand(Commodity commodity) {
        return Optional.ofNullable(demands.get(commodity));
    }
}

The other option, which might make more sense if you need to, for example, track a Demand as a distinct class in your model, would be for Person to have a set of Demand s, with each Demand referencing a Commodity and a count.另一个选项可能更有意义,例如,如果您需要将Demand跟踪为 model 中的不同 class,那么Person将拥有一组Demand ,每个Demand引用Commodity计数。 So, given the same Commodity class above, something like this:所以,给定上面相同的Commodity class,是这样的:

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class Demand {

    private Commodity commodity;
    private int count;
}
import java.util.LinkedList;
import java.util.List;

public class Person {

    private List<Demand> demands = new LinkedList<>();

    public void addDemand(Commodity commodity, Integer count) {
        addDemand(new Demand(commodity, count));
    }

    public void addDemand(Demand demand) {
        demands.add(demand);
    }
}

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

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