一个ArrayList添加了2个不同的选项

时间:2018-01-11 19:27:43

标签: java arraylist

我的问题是我必须使用一个Arraylist并添加两个选项

1.Task(内容,startTime,startEnd,难度)

contents- String
startTime,startEnd- Localtime
difficulty- boolean easy/difficult
  1. 发生(内容,开始时间,开始结束,重要,训练)
  2. contents- String
    startTime,startEnd- Localtime
    important- boolean Yes/No
    training- boolean  Yes/No
    

    是否可以将两个不同的选项合并为一个arraylist ??

2 个答案:

答案 0 :(得分:1)

公共属性的接口

public interface MyI {

    public String getContens();

    public LocalTime getStartTime();

    public LocalTime getStartEnd();

}

然后为唯一实例的范围创建模型:

public class Task implements MyI {

    private String contens;

    private LocalTime startTime;

    private LocalTime startEnd;

    private boolean difficulty;

    public boolean isDifficulty() {
        return difficulty;
    }

    public void setDifficulty(boolean difficulty) {
        this.difficulty = difficulty;
    }

    public void setContens(String contens) {
        this.contens = contens;
    }

    public void setStartTime(LocalTime startTime) {
        this.startTime = startTime;
    }

    public void setStartEnd(LocalTime startEnd) {
        this.startEnd = startEnd;
    }

    @Override
    public String getContens() {
        return this.contens;
    }

    @Override
    public LocalTime getStartTime() {
        return this.startTime;
    }

    @Override
    public LocalTime getStartEnd() {
        return this.startEnd;
    }

}

public class Happening implements MyI {

    private String contens;

    private LocalTime startTime;

    private LocalTime startEnd;

    private boolean important;

    private boolean training;

    public boolean isImportant() {
        return important;
    }

    public void setImportant(boolean important) {
        this.important = important;
    }

    public boolean isTraining() {
        return training;
    }

    public void setTraining(boolean training) {
        this.training = training;
    }

    public void setContens(String contens) {
        this.contens = contens;
    }

    public void setStartTime(LocalTime startTime) {
        this.startTime = startTime;
    }

    public void setStartEnd(LocalTime startEnd) {
        this.startEnd = startEnd;
    }

    @Override
    public String getContens() {
        return this.contens;
    }

    @Override
    public LocalTime getStartTime() {
        return this.startTime;
    }

    @Override
    public LocalTime getStartEnd() {
        return this.startEnd;
    }

}

现在,您可以使用界面创建一个列表来保存两种类型的模型:

List<MyI> myList = new ArrayList<MyI>();

答案 1 :(得分:0)

您可以为这些类定义一个公共超类。例如,抽象类或接口,然后您将在该超类或接口上创建List。