MVC的推拉模型有什么区别?
是Struts2,Spring MVC Pull?
考虑使用Java Puzzlers中的代码
class Gloam<T>{
String glom(Collection<?> objs ) {
System.out.println("collection");
String result = "";
for (Object o : objs ){
result += o;
}
return result;
}
int glom(List <Integer> ints ) {
System.out.println("List");
int result = 0;
for ( int i : ints )
result += i ;
return result;
}
public static void main(String[] args) {
List<String> strings = Arrays.asList("1", "2", "3");
System.out.println(new Gloam().glom(strings));
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序时,它会给出类强制转换异常,但是如果我在main方法中为Gloam类提供任何Generic参数,它可以正常工作.
public static void main(String[] args) {
List<String> strings = Arrays.asList("1", "2", …Run Code Online (Sandbox Code Playgroud) 在Enum中value()方法如何工作?
values()方法背后的逻辑是什么?
在我的项目中,我们将所有枚举数据缓存在Map中,如下所示:
public enum Actions {
CREATE("create"),
UPDATE("update"),
DELETE("delete"),
ACTIVE("active"),
INACTIVE("inactive"),
MANAGE_ORDER("manage"),
;
private static Map<String, Actions> actionMap;
static {
actionMap = new HashMap<String, Actions>(values().length);
for(Actions action : values()) {
actionMap.put(action.getName(), action);
}
}
public static Actions fromName(String name) {
if(name == null)
throw new IllegalArgumentException();
return actionMap.get(name);
}
private String name;
private Actions(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
这是使用枚举的最佳做法吗?