解释用例:
public class Course {
Mathematics math;
Physics physics;
English english;
String teacher;
String schoolName; //Just want explaing modelling hence keeping
}
public class PhysicsHelper extends Helper {
public void help(Physics physics, String teacher) {
Course course = // create course
help(course);
}
}
public class MathematicsHelper extends Helper {
public void help(Mathematics math, String teacher, Address address) {
String schoolName = address.getSchoolName
Course course = // create course
help(course);
}
}
public abstract class Helper {
public void help(Course couse) {
// defualt set of operations to be called by all helpers.
}
}
现在,我有一个类似的要求:
PhysicsHelper也开始了解英语, 所以现在PhysicsHelper有方法
public class PhysicsHelper extends Helper {
public void help(Physics physics, String teacher, English english) {
}
}
答案 0 :(得分:2)
正如@Charles Follet建议的那样,最好在help(...)
中重载PhysicsHelper.class
方法,而不是在一种方法中处理所有可能的主题。另外,您可以使Course.class
仅包含所有当前主题的共享字段。
public class Course {
String teacher;
String schoolName; //Just want explaing modelling hence keeping
}
public class EnglishCourse extends Course {
English english;
}
public class PhysicsCourse extends Course {
Physics physics;
}
public class PhysicsHelper extends Helper {
public void help(Physics physics, String teacher) {
}
public void help(English english, String teacher) {
}
}