我有一个接口Medicine
,我为此创建了几个实例。让我们来看看,
interface Medicine {
Medicine Antibiotic = new Medicine() {
@Override
public int getCountOfTuberculous(QuarantineTwo quarantineTwo) {
return quarantineTwo.tuberculous().getSize();
}
/**
* Antibiotic cures the tuberculous
*
* @param q
*/
@Override
public void on(QuarantineTwo q) {
int initialNumOfTuberculous = getCountOfTuberculous(q);
System.out.println("Numbe of perople have Tuberculous before treated w/ Antibiotic = " + initialNumOfTuberculous);
q.tuberculous().changeHealthStatus(q.healthy());
}
@Override
public Treatment combine(Treatment treatment) {
return treatment.plus(this);
}
@Override
public String toString() {
return "Antibiotic";
}
};
Medicine Insulin = new Medicine() {
// cant use this method as it will provide the number of Tuberculous 0
// because, initially, the Quarantine was treated with Antibiotic
@Override
public int getCountOfTuberculous(QuarantineTwo quarantineTwo) {
return quarantineTwo.tuberculous().getSize();
}
@Override
public void on(QuarantineTwo q) {
if (isInsulinCombinedWithAntibiotic(q.getTreatment())) {
q.healthy().changeHealthStatus(q.feverish());
// q.healthy().changeHealthStatus(q.feverish(), iniNumOfTuberculous);
} else {
// Prevent None effects, done is this.combine
}
}
@Override
public Treatment combine(Treatment treatment) {
return treatment.remove(Medicine.None)
.plus(this);
}
/**
* helper method to see whether the Insulin is combined with Antibiotic
*
* @param treatment
* @return
*/
private boolean isInsulinCombinedWithAntibiotic(Treatment treatment) {
return treatment.contains(this) &&
treatment.contains(Medicine.Antibiotic);
}
@Override
public String toString() {
return "Insulin";
}
};
void on(QuarantineTwo quarantineTwo);
Treatment combine(Treatment treatment);
int getCountOfTuberculous(QuarantineTwo quarantineTwo);
}
现在,当我进行测试时,我可能会这样打电话,
@Test
public void antibioticPlusInsulin() throws Exception {
quarantine.antibiotic();
quarantine.insulin();
assertEquals("F:3 H:1 D:3 T:0 X:0", quarantine.report());
}
这两行代码意味着我们将抗生素和胰岛素的治疗程序与检疫系统相结合,影响应该是累积的。
quarantine.antibiotic();
quarantine.insulin();
因此,我想跟踪Antibiotic
最初存储在initialNumOfTuberculous
中的结核病的q.healthy().changeHealthStatus(q.feverish(), iniNumOfTuberculous);
治愈了多少人,并使用该值进行呼叫
iniNumOfTuberculous
这个呼吁假设将所有人从健康状态改为发烧状态,但最初用结核病治愈。
如何将Medicine Antibiotic
的值存储在Medicine Insulin
中,并在{{1}}中提供?
答案 0 :(得分:1)
听起来你需要一个抽象类
abstract class AbstractMedicine implements Medicine {
protected int iniNumOfTuberculous;
}
public class Insulin extends AbstractMedicine {
// can use iniNumOfTuberculous here
}
注意:共享变量定义的可用性;价值本身不是。
我不认为你应该在界面中实现你的具体类,顺便说一句