JAVA - 如何设置不同类的相同字段

时间:2018-05-29 15:23:39

标签: java amazon-web-services amazon-dynamodb

我有一些模型类,这些类有两个相同的字段:已批准和已检查,两者都设置为false。我想创建一个端点,将此字段设置为true,无论它是哪种类型的obcject。例如两个模型类:

public class Meal extends Request {

private String id;
private Integer calories;
private LocalDateTime mealTime;
private Double fat;
private Double protein;
private Double carbon;
private Integer weight;
private List<MealElement> mealElements;
private MealStatus mealStatus;
private MealKind mealKind;
private List<Recipe> recipes;
private Boolean approved;
private Boolean checked;

public class Training extends Request {

private String id;
private String trainingName;
private Integer predictedCaloriesToBurn;
private Integer burnedCalories;
private LocalDateTime startTime;
private LocalDateTime endTime;
private List<Exercise> exerciseList;
private List<TrainingLabels> trainingLabels;
private Boolean approved;
private Boolean checked;
private ActivityDay activityDay;

我正在使用AWS创建这个,所以我必须创建lambda来为此创建端点。有人可以帮我理解怎么做吗?

3 个答案:

答案 0 :(得分:0)

一个好方法是使用一个或两个接口,如下所示

public interface Checkable {
   setCheck(boolean ckeck);
} 

public interface Approvable {
   setApproved(boolean approved);
}

public class Meal extends Request implements Checkable, Approvable {
   ...
}

public class OtherClass implements Checkable, Approvable {
}



...



// And where you need to use it

public void setChecked(Checkable checkable, boolean value) {
   checkable.setCheck(value);
}

// Calling it as follow
Meal meal = ...
setChecked(meal, true);

答案 1 :(得分:0)

也许界面可以提供帮助?

public interface IdSupport {
    public void setId(String id);
    public String getId();
}

public class Meal extends Request implements IdSupport {
   ... 
}
public class Training extends Request implements IdSupport {
   ... 
} 

答案 2 :(得分:0)

继承

公共类应该提供一个接口。就像提供两种setter方法一样。它还应包含字段,因为它们是公共类的一部分,而不是特定于子类。

public class Request {
    private boolean approved;
    private boolean checked;

    public void setApproved(boolean approved) {
        this.approved = approved;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public boolean isApproved() {
        return approved;
    }

    public boolean isChecked() {
        return checked;
    }
}

您的端点就可以这样做:

someRequest.setApproved(true);
someRequest.setChecked(true);

它适用于Request类型的每个类,包括您的子类MealTraining

接口

如果将其移至InspectionItem

等界面,情况会更好
public interface InspectionItem {
    void approve();
    void check();
    boolean isApproved();
    boolean isChecked();
}

然后你让公共类实现:

public class Request implements InspectionItem {
    private boolean approved;
    private boolean checked;

    @Override
    public void approve() {
        approved = true;
    }

    @Override
    public void check() {
        checked = true;
    }

    @Override
    public boolean isApproved() {
        return approved;
    }

    @Override
    public boolean isChecked() {
        return checked;
    }
}

然后您的端点适用于InspectionItem类型的所有内容,而不仅仅是Request s:

someItem.approve();
someItem.check();