[英]Java - Get correctly casted object from HashMap
我正在开发一个具有多个系统的应用程序(在我的例子中,系统是我的应用程序中的可加载组件,它处理特定任务,例如:翻译,配置处理,音频等......)。 这些系统共享一些常用方法,因此我创建了一个接口:
public interface System {
public void initialize();
public void uninitialize();
}
但是每个系统都有自己的方法,对应于系统实现的任务。 这是我的系统的两个例子
第一个存储设置:
public class SettingsSystem implements System {
@Override
public void initialize() {
// few lines of code here
}
@Override
public void uninitialize() {
// few lines of code here
}
public Object getSetting(String key) {
// returns a setting
}
}
和翻译的语言系统。
public class LanguageSystem implements System {
@Override
public void initialize() {
// few lines of code here
}
@Override
public void uninitialize() {
// few lines of code here
}
public void setLanguage(String language) {
// sets language
}
public boolean load( String name) {
// loads a file
}
public String translate(String key) {
// returns translation of a key
}
}
我将System实例存储在静态HashMap中,因为我想在我的应用程序的任何地方访问它们。 我创建了一个静态方法来通过密钥返回任何系统。
public class App {
// note: System is the System interface I created
private static Map<String, System> systems = new HashMap<String, System>();
public static Object getSystem(String key) {
return systems.get(key);
}
}
如果我调用App.getSystem()方法,它会返回一个Object。 所以如果我打电话给...
App.getSystem("settings");
...它将返回正确的项目作为对象。 但问题是,我无法调用SettingsSystem的getSetting()方法,因为它告诉我没有这样的方法。
如果我这样实现它:
public static System getSystem(String key) {
return systems.get(key);
}
我可以调用initialize()或uninitialize()方法,但仍然没有getSettings()方法可用。
我想要的是当我调用App.getSystem()方法时,我希望它返回其中包含的正确类型的对象。 例:
App.getSystem("settings"); // I want it to return a SettingsSystem object
// So I can do:
String width = App.getSystem("settings").getSetting("width");
App.getSystem("language"); // I want it to return a LanguageSystem object
// So I can do:
String welcome = App.getSystem("language").translate("hello_world");
这怎么可能? 提前致谢。
UPDATE
根据您的帮助,我决定为每个系统声明getter方法:
public SettingsSystem getSettingsSystem() {
return (SettingsSystem) system.get("settings");
}
谢谢你的建议!
你需要像这样强制转换为正确的类型:
SettingsSystem settingSystem = null;
System system = App.getSystem("settings");
if (system instanceof SettingsSystem) {
settingSystem = (SettingSystem) system;
}
settingSystem.getSetting("width");
当然,使用相关的null
检查等来执行此操作。
如果可以避免铸造,我更喜欢。 您可以将两个单独的地图与实施类保持一致吗?
或者,由于您似乎有几个System
实现,请使用Patricia Shanahan的解决方案#2,它可以隔离转换。
表达式的类型(包括调用返回的类型)在编译时是固定的。 你不能在同一个对象中使用相同的方法,有几种不同的返回类型。
您有两种选择:
那是因为System
类没有那些方法。 您需要显式转换为特定的类SettingSystem
和LanguageSystem
才能使用此方法。
getSystem()方法返回一个System实例,因此您只能访问System接口中定义的方法。
如果要访问具体实现类中定义的方法,则必须首先将该对象强制转换为该类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.