检查枚举值,然后添加另一个字段(Java)
我有一个Java课程,名为Course。在该类中,我有一些私有字段(ID、名称、教师列表和枚举类型(可以是必修、选修或模块化的))。如果课程类型是模块化的,那么应该有另一个现场模块。如何以不同的方式实现这一点(不要有3种不同的课程:强制课程、选修课程和模块课程)?好吧,你可以有一个额外的字段检查枚举值,然后添加另一个字段(Java),java,enums,field,Java,Enums,Field,我有一个Java课程,名为Course。在该类中,我有一些私有字段(ID、名称、教师列表和枚举类型(可以是必修、选修或模块化的))。如果课程类型是模块化的,那么应该有另一个现场模块。如何以不同的方式实现这一点(不要有3种不同的课程:强制课程、选修课程和模块课程)?好吧,你可以有一个额外的字段模块,这是可选的。因此它将是null,它的getter返回可选的(这样可以避免null指针异常) 您可以有一个额外的字段模块,这是可选的。因此它将是null,它的getter返回可选的(这样可以避免null指
模块,这是可选的。因此它将是null
,它的getter返回可选的
(这样可以避免null指针异常) 您可以有一个额外的字段模块
,这是可选的。因此它将是null
,它的getter返回可选的
(这样可以避免null指针异常) 这似乎是工厂或战略模式的一个例子。然而,这需要多态性。
向类动态添加字段是不可能的,或者更好,但这是一个字节码操作主题
您可以模拟模块
类型字段的可选性,方法是通过可选的
getter方法将其封装并公开,或者对其应用一种无操作
实现。这似乎是工厂或策略模式的一种情况。然而,这需要多态性。
向类动态添加字段是不可能的,或者更好,但这是一个字节码操作主题
您可以模拟模块
类型字段的可选性,方法是通过可选
getter方法对其进行封装和公开,或者对其应用一种无操作
实现。而不是使用null
或类似方法
使用类型,而不是使用类型的枚举
abstract class CourseType {
...
}
class Modular extends CourseType {
private Module module;
...
}
class Compulsory extends CourseType {
...
class Course {
private CourseType type;
除了使用null
或类似的
使用类型,而不是使用类型的枚举
abstract class CourseType {
...
}
class Modular extends CourseType {
private Module module;
...
}
class Compulsory extends CourseType {
...
class Course {
private CourseType type;
公共课{
私有UUID;
私有字符串名称;
私人名单教师;
私有类型;
私有字符串模块;
公共无效类型(强制){
类型(type.employment,null);
}
公共图书馆{
类型(type.optional,null);
}
公共void类型模块(字符串模块){
对象。requirennull(模块);
类型(类型、模块、模块);
}
私有void类型(类型类型,字符串模块){
this.type=type;
this.module=模块;
}
公共枚举类型{
强制性的
选修的
模块化
}
}
公共课{
私有UUID;
私有字符串名称;
私人名单教师;
私有类型;
私有字符串模块;
公共无效类型(强制){
类型(type.employment,null);
}
公共图书馆{
类型(type.optional,null);
}
公共void类型模块(字符串模块){
对象。requirennull(模块);
类型(类型、模块、模块);
}
私有void类型(类型类型,字符串模块){
this.type=type;
this.module=模块;
}
公共枚举类型{
强制性的
选修的
模块化
}
}
使用枚举是对象这一事实。添加一个专用字段“module”和一个方法“getModule”,该方法将为模块课程返回模块值,为必修课和选修课返回null
enum courseType{
Modular("module x"), Compulsory, Elective;
private String module = null;
private courseType(){}
private courseType(String module){this.module = module;}
public String getModule() {return this.module;}
}
您也可以改为引发异常:
enum courseType{
Modular("module x"),
Compulsory {public String getModule() { throw new AssertionError("not applicable");}},
Elective {public String getModule() { throw new AssertionError("not applicable");}};
private final String module;
private courseType(){ this.module = null;}
private courseType(String module){this.module = module;}
public String getModule() { return this.module;}
}
使用枚举是对象这一事实。添加一个专用字段“module”和一个方法“getModule”,该方法将为模块课程返回模块值,为必修课和选修课返回null
enum courseType{
Modular("module x"), Compulsory, Elective;
private String module = null;
private courseType(){}
private courseType(String module){this.module = module;}
public String getModule() {return this.module;}
}
您也可以改为引发异常:
enum courseType{
Modular("module x"),
Compulsory {public String getModule() { throw new AssertionError("not applicable");}},
Elective {public String getModule() { throw new AssertionError("not applicable");}};
private final String module;
private courseType(){ this.module = null;}
private courseType(String module){this.module = module;}
public String getModule() { return this.module;}
}