java 如何设计一个名为 allergy 的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29269380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I design a class named allergy?
提问by user3780902
requirement: design a class named allergy that provides information about the allergy of a patient. e.g. who reported the allergy(patient/doctor/relative), different symptoms of the allergy that are detected, severity, method that returns when was that allergy detected in that patient.
要求:设计一个名为 allergy 的类,提供有关患者过敏的信息。例如,谁报告了过敏症(患者/医生/亲属)、检测到的过敏症的不同症状、严重程度、返回该患者何时检测到过敏症的方法。
I am thinking of something like this:
我在想这样的事情:
public abstract class Allergy{
private String reporter;
private String symptoms;
private int timeReported;
private int severity;
//Higher the number, higher the severity
public Allergy(String reporter, String symptoms, int timeReported, int severity){
this.reporter=reporter;
this.symptoms=symptoms;
this.timeReported=timeReported;
this.severity=severity;
}
public void setReporter(String reporter){
this.reporter=reporter;
}
public void setSymptoms(String symptoms){
this.symptoms=symptoms;
}
public void setSeverity(int severity){
this.severity=severity;
}
public void setTimeReported(int timeReported){
this.timeReported=timeReported;
}
public String getReporter(){
return reporter;
}
public String getSymptoms(){
return symptoms;
}
public int getSeverity(){
return severity;
}
public int getTimeReported(){
return timeReported;
}
}
Is this a good design of the class? Is there any way I can improve the design? Or does someone have a better implementation?
这是一个很好的班级设计吗?有什么办法可以改进设计吗?或者有人有更好的实现吗?
I have to be able to explain as many OOP concepts. Can I make use of any other OOP concept here apart from abstract, encapsulation and inheritance that I will be able to use having the current design in my mind?
我必须能够解释尽可能多的 OOP 概念。除了抽象、封装和继承之外,我可以在这里使用任何其他 OOP 概念,我可以在脑海中使用当前的设计吗?
回答by Rani
how about using an enum for severity(low, high, medium) and using an enum for the reporter(patient, doctor, familymember, other) and using HashSet for Symptoms.
如何对严重性(低、高、中)使用枚举,对报告者(患者、医生、家庭成员、其他)使用枚举,并对症状使用 HashSet。
回答by Anand Kulkarni
Best way to have Pojo of allergy and patient. Patient has allergy. However; patient may have allergies then you can add data structure like arrayList of allergy.
最好的方式有 Pojo 的过敏和耐心。患者过敏。然而; 患者可能有过敏症,那么您可以添加诸如过敏症的arrayList之类的数据结构。
回答by Ueli Hofstetter
Apart from the syntax errors mentioned in the comment, and assuming you should not use inheritance, the concept you are missing most are interfaces. So what you could include an interface Disease (called IDisease) and let the Allergy class implement it, i.e.
除了评论中提到的语法错误,假设你不应该使用继承,你最缺少的概念是接口。那么你可以包含一个接口疾病(称为 IDisease)并让 Allergy 类实现它,即
public interface IDisease{
void setReporter(String reporter);
//other methods
}
}
public class Allergy implements IDisease{
public Allergy(String reporter, String symptoms, int timeReported, int severity){
this.reporter=reporter;
this.symptoms=symptoms;
this.timeReported=timeReported;
this.severity=severity;
}
public void setReporter(String reporter){
this.reporter=reporter;
}
public void setSymptoms(String symptoms){
this.symptoms=symptoms;
}
public void setSeverity(int severity){
this.severity=severity;
}
public void setTimeReported(int timeReported){
this.timeReported=timeReported;
}
public String getReporter(){
return reporter;
}
public String getSymptoms(){
return symptoms;
}
public int getSeverity(){
return severity;
}
public int getTimeReported(){
return timeReported;
}
}
Using an interface here could be justified in case later on you would like to write an algorithm to analyse all different kinds of diseases (including Allergies) and would like to keep the algorithm decoupled from the actual details of a particular disease.
如果稍后您想编写算法来分析所有不同类型的疾病(包括过敏症),并希望使算法与特定疾病的实际细节分离,则此处使用接口可能是合理的。