我有一个对象。现在,仅说两年,它的吸气剂和吸气剂

private String mYearOne;
private String mYearTwo;
public String getmYearOne() {
    return mYearOne;    }
public void setmYearOne(String mYearOne) {
    this.mYearOne = mYearOne;   }
public String getmYearTwo() {
    return mYearTwo;    }
public void setmYearTwo(String mYearTwo) {
    this.mYearTwo = mYearTwo;   }


然后每年有三个保险计划。以及它的获取器和设置器。

private String healthPlan;
private String carPlan;
private String housePlan;
private String healthPlanTwo;
private String carPlanTwo;
private String housePlanTwo;
public String getHealthPlan() {
    return healthPlan;  }
public void setHealthPlan(String healthPlan) {
    this.healthPlan = healthPlan;   }
public String getCarPlan() {
    return carPlan; }
public void setCarPlan(String carPlan) {
    this.carPlan = carPlan; }
public String getHousePlan() {
    return housePlan;   }
public void setHousePlan(String housePlan) {
    this.housePlan = housePlan; }
public String getHealthPlan() {  //For the second year
    return healthPlan;  }
public void setHealthPlan(String healthPlan) {
    this.healthPlan = healthPlan;   }
public String getCarPlan() {
    return carPlan; }
public void setCarPlan(String carPlan) {
    this.carPlan = carPlan; }
public String getHousePlan() {
    return housePlan;   }
public void setHousePlan(String housePlan) {
    this.housePlan = housePlan; }
public String getHealthPlanTwo() {
    return healthPlanTwo;   }
public void setHealthPlanTwo(String healthPlanTwo) {
    this.healthPlanTwo = healthPlanTwo; }
public String getCarPlanTwo() {
    return carPlanTwo;  }
public void setCarPlanTwo(String carPlanTwo) {
    this.carPlanTwo = carPlanTwo;   }
public String getHousePlanTwo() {
    return housePlanTwo;    }
public void setHousePlanTwo(String housePlanTwo) {
    this.housePlanTwo = housePlanTwo;   }


您会注意到代码很庞大。我需要在<list>年中定义它们。因此,如果考虑10年,我将乘以10
乘以3 = 30个计划及其吸气剂和吸气剂。

怎么办呢?

最佳答案

我认为您最好的选择是维护保险计划的年数和阵列列表。这样,您可以一次获取阵列列表,并获取实际需要的年份的保险计划详细信息。这将以单个保险计划阵列列表和多年的单个阵列列表为特征。

私有ArrayList mYear;
私有ArrayList healthPlan;
私有ArrayList carPlan;
私有ArrayList housePlan;

公共字符串getHousePlanForYear(字符串年份){
返回housePlan.get(mYear.indexOf(year));
}

public void setHousePlanForYear(String housePlan,String year){
this.housePlan.set(mYear.indexOf(year),housePlan);
}



其他计划也是如此。当然,所有这些都是假设年份始终存在以及其他边界条件。只需在这些getter和setter中添加边界检查,您就可以开始使用。 :)

09-26 15:34