用于多次比较的 String.equals 的替代方案(不带枚举)
我曾考虑过为我的所有字段创建枚举,但这看起来也不酷(我有许多实现类似方法的类)。有更好的办法吗?
public void writeAttribute(String attribute, Object value) {
if (attribute.equals("title")) {
title = (String) value;
} else if (attribute.equals("description")) {
description = (String) value;
} else if (attribute.equals("room")) {
room = (Room) value;
} else if (attribute.equals("type")) {
type = AppointmentType.valueOf((String) value);
} else if (attribute.equals("guestCount")) {
guestCount = (Integer) value;
}
}
根据attribute
参数,我想将输入value
映射到适当的字段。有没有办法清理/优化我的代码?为每个字段编写 .equals
不太优雅。
I have thought about making enums for all my fields, but that doesn't look to cool either (i have many classes that implements similar methods). Is there a better way?
public void writeAttribute(String attribute, Object value) {
if (attribute.equals("title")) {
title = (String) value;
} else if (attribute.equals("description")) {
description = (String) value;
} else if (attribute.equals("room")) {
room = (Room) value;
} else if (attribute.equals("type")) {
type = AppointmentType.valueOf((String) value);
} else if (attribute.equals("guestCount")) {
guestCount = (Integer) value;
}
}
Depending on the attribute
parameter, i want to map the input value
to the appropriate field. Is there a way to clean up/optimize my code? Writing .equals
for every field isn't too elegant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您是否考虑过使用 HashMap 来存储属性,而不是为每个属性使用单独的命名成员变量?您的类将有一个属性映射
,并且上面的方法简化为
由于您无论如何都在进行强制转换,因此您将拥有在访问它时适当地强制转换该值的方法,或者在访问点自行强制转换它。
Rather than have an individual named member variable for each attribute, have you considered using a HashMap to store attributes? Your class would have an attribute map
and your method above reduces to
Since you're casting anyway, you would then have methods that cast the value appropriately when you access it, or cast it yourself at the point of access.
您可以将属性/值对放入映射中:
然后将它们映射到字段会更清晰一些:
You could put your attribute/value pairs in a map:
And then mapping them to fields is a little cleaner:
你可以这样做:(随意使用除 Runnable 之外的接口)
you could do this: (feel free to use interface other than Runnable)
我认为如果你的方法不能避免这样的 if 条件,请尝试做类似的事情:
其中 TITLE 和DESCRIPTION 是你的最终变量。如果您的地图中没有值(例如描述),那么您的值将为空。
I don't think if your method is OK to avoid such if-conditions, try do something like that:
where TITLE and DESCRIPTION is your final variables. If you won't have value in your Map for example in description then you'll have there null.
一种方法是使用反射,请查看这篇帖子,它解释了如何获取字段然后为其设置值。
One way is using reflection, check out this post which explains how to get a field and then set the value for it.
你可以尝试
You can try
我们编写了预处理的 Java,您可以找到 处理代码在这里。
它为我们提供了字符串开关。然而,它增加了一个额外的编译步骤。需要处理的文件被命名为
Foo.jpp
并被处理为Foo.java
,但是基于String
的普遍性在我们的代码中切换更多从而弥补了这一不便。例子:
We write preprocessed Java, for which you can find the processing code here.
It gives us string switches. However it adds an additional step to compilation. Files that require processing are named
Foo.jpp
and are processed toFoo.java
, but the pervasiveness ofString
-based switches in our code more than makes up for this inconvenience.Example:
从 Java SE 7(2011 年 7 月 28 日)开始,您可以切换字符串。
Since Java SE 7 (July 28, 2011) you can switch strings.