[英]refactoring and removing 'for' statements when circling over many enum structures
My class have an Enum as attribute. 我的班级有一个Enum作为属性。 It uses a String to set this attr. 它使用字符串来设置此属性。 There are many Enum classes. 有许多枚举类。 How can I refactor it? 我该如何重构?
public void setType(String s) {
for (MyEnum1 e : MyEnum1.values()) {
if (e.name().equalsIgnoreCase(s))
this.type = e;
}
for (MyEnum2 e : MyEnum2.values()) {
if (e.name().equalsIgnoreCase(s))
this.type = e;
}
for ...
Thanks in advance. 提前致谢。
使用MyEnum1.valueOf(String)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.