Java – 从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"); } 谢谢你的建议! 解决方法
表达式的类型(包括调用返回的类型)在编译时是固定的.你不能在同一个对象中使用相同的方法,有几种不同的返回类型.
您有两种选择: >返回类型Object,并在调用者中强制转换.>为每种类型声明一个不同的getXXXSystem方法. getLanguageSystem将返回类型LanguageSystem. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |