Java 通用意外令牌
Java generic unexpected token
我有一个通用的 class "Property",我想将其放入 HashMap,但出现 "Unexpected token: >"
错误。
我正在使用 Processing 2.2.1.
class MouseEvent extends Event{
HashMap<String, Property> Args;
MouseEvent(String type){
Args = new HashMap<String, Property>();
Args.put("mouseX", new Property<int>(mouseX)); //throws unexpected token
Args.put("mouseY", new Property<int>(mouseY));
Args.put("Button", new Property<int>(mouseButton));
Args.put("Type", new Property<String>(type));
}
}
class Property<T>{
private T val;
Poperty(T v){
val = v;
}
void Set(T v){
val = v;
}
T Get(){
return val;
}
}
我在这里误解了什么? :/
您不能使用像 int
这样的原始数据类型作为通用类型。您必须使用相应的对象,即 Integer
.
将您的代码更改为
Args.put("mouseX", new Property<Integer>(mouseX));
另外在旁注中遵循 java 命名约定。 Variable/method 名称应采用驼峰式
HashMap<String, Property> args;
args = new HashMap<String, Property>();
//...
我有一个通用的 class "Property",我想将其放入 HashMap,但出现 "Unexpected token: >"
错误。
我正在使用 Processing 2.2.1.
class MouseEvent extends Event{
HashMap<String, Property> Args;
MouseEvent(String type){
Args = new HashMap<String, Property>();
Args.put("mouseX", new Property<int>(mouseX)); //throws unexpected token
Args.put("mouseY", new Property<int>(mouseY));
Args.put("Button", new Property<int>(mouseButton));
Args.put("Type", new Property<String>(type));
}
}
class Property<T>{
private T val;
Poperty(T v){
val = v;
}
void Set(T v){
val = v;
}
T Get(){
return val;
}
}
我在这里误解了什么? :/
您不能使用像 int
这样的原始数据类型作为通用类型。您必须使用相应的对象,即 Integer
.
将您的代码更改为
Args.put("mouseX", new Property<Integer>(mouseX));
另外在旁注中遵循 java 命名约定。 Variable/method 名称应采用驼峰式
HashMap<String, Property> args;
args = new HashMap<String, Property>();
//...