【发布时间】:2022-01-18 16:37:17
【问题描述】:
我有一个带有一些链接的枚举。在这些链接中是相同的版本号(1.1.1),现在作为一个字符串,我想用常量替换它。这个怎么做?
我可以在返回链接时编辑链接(用我的常量替换一些字符串),但这似乎不是干净的解决方案。感谢您的帮助!
package test.intro;
import test.version;
public enum LinkType
{
LINK1("test/1.1.1/doc/test1.pdf"),
LINK2("test/1.1.1/doc/test2.pdf"),
LINK3("test/1.1.1/doc/test3.pdf");
public final String href;
//my constant I want to use:
private final String versionName = version.getVersionName();
private LinkType(String href)
{
this.href = href;
}
public String getHref()
{
return href;
}
}
【问题讨论】:
-
为什么?如果它对所有枚举都具有相同的值,为什么要在其中拥有它
-
@Stultuske 嗨,因为在
test.version我可以在一天更新版本,我不想手动更新枚举中的所有链接。