【发布时间】:2010-03-26 09:32:10
【问题描述】:
我想知道在 Java 中是否有一个优雅的解决方案(除了明显的 - 声明不同/显式函数。这是代码:
private static HashMap<String, Integer> nameStringIndexMap
= new HashMap<String, Integer>();
private static HashMap<Buffer, Integer> nameBufferIndexMap
= new HashMap<Buffer, Integer>();
// and a function
private static String newName(Object object,
HashMap<Object, Integer> nameIndexMap){
....
}
问题是我无法将 nameStringIndexMap 或 nameBufferIndexMap 参数传递给函数。除了执行另一个明确需要 HashMap
我的问题是: 这可以用更优雅的解决方案/使用泛型或类似的东西来实现吗?
谢谢,
尤利安
【问题讨论】:
-
我不明白你的目标是什么。你想为 Buffers 复制你的 newName 方法吗?
-
我想对这两种类型使用相同的功能。这就是我想要达到的目标。下面有一些答案可以帮助我做到这一点
标签: java generics inheritance