实现addDiscount方法,将DiscountData作为输入,将其存储在订阅对象

时间:2018-01-25 21:35:25

标签: java arraylist

我有以下两个班级。我需要在第二个类中实现一个方法,即addDiscount,它将DiscountData作为输入存储在订阅对象中。

我猜我需要在这里使用arraylist?如果是这样,怎么样?

DiscountData类:

class DiscountData {
    private int value;
    private int length;

public DiscountData(int value, int length) {
    this.value = value;
    this.length = length;
}

public int getValue() {
    return this.value;
}

public void setValue(int value) {
    this.value = value;
}

public int getLength() {
    return this.length;
}

public void setLength(int length) {
    this.length = length;
}
}

订阅类:

public class Subscription {
    public void addDiscount(DiscountData discountData) {
    // your code here
    }
}

1 个答案:

答案 0 :(得分:0)

像这样的Smth:

public class Subscription {
    private List<DiscountData> data = new ArrayList<DiscountData>();

    public void addDiscount(DiscountData discountData) {
       data.add(discountData);
    }
}