这个 Java HashMap 声明有什么问题?简单点
private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();
看起来很无辜,但 Eclipse 却在抱怨分号。
Eclipse 错误:标记“;”上的语法错误,无效的AssignmentOperator
谢谢
整个类:
package persistence;
import java.util.UUID;
import java.util.HashMap;
import domain.Task;
public class PersistanceFacade {
private static PersistanceFacade uniqueInstance = null;
private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();
(SingleTaskRDBMapper) mapper.put("SingleTask", new SingleTaskRDBMapper());
public PersistanceFacade() {};
public static synchronized PersistanceFacade getUniqueInstance() {
if (uniqueInstance == null) {
uniqueInstance = new PersistanceFacade();
return uniqueInstance;
}
else return uniqueInstance;
}
@SuppressWarnings("unchecked")
public Object get(UUID oid, Class type) {
Object mappers;
IMapper mapper = ((HashMap<String, IMapper>) mappers).get(type);
}
}
private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();
Seems so innocent yet Eclipse is complaining about the semi-colon, of all things.
Eclipse error: Syntax error on token ";", invalid AssignmentOperator
Thanks
The entire class:
package persistence;
import java.util.UUID;
import java.util.HashMap;
import domain.Task;
public class PersistanceFacade {
private static PersistanceFacade uniqueInstance = null;
private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();
(SingleTaskRDBMapper) mapper.put("SingleTask", new SingleTaskRDBMapper());
public PersistanceFacade() {};
public static synchronized PersistanceFacade getUniqueInstance() {
if (uniqueInstance == null) {
uniqueInstance = new PersistanceFacade();
return uniqueInstance;
}
else return uniqueInstance;
}
@SuppressWarnings("unchecked")
public Object get(UUID oid, Class type) {
Object mappers;
IMapper mapper = ((HashMap<String, IMapper>) mappers).get(type);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
线路
没到位。您应该将其放在构造函数中:
另请注意,我删除了构造函数右大括号后的无关分号。我也不确定 get 方法中发生了什么。您声明了对象
mappers
,但从未初始化它。这肯定会导致错误。The line
is out of place. You should put that in the constructor:
Also notice I removed the extraneous semicolon after the closing brace of the constructor. I'm also not sure what is going on in the get method. You declare the object
mappers
but never initialize it. That will certainly cause an error.不确定 IMapper 是什么,我假设是某个接口,但我只是将其放入 Eclipse 中,它不会给我任何错误:
也许您可以发布更多代码?
Not sure what IMapper is, some interface I assume, but I just dropped this into Eclipse and it doesn't give me any errors:
Perhaps you could post more of your code?
这段代码完美运行:),受到 laz 帮助的启发
This code works perfectly :), inspired by help from laz