这个对于性能来说,是没有任何作用的,但对于mvc的实现缺有着非常重要的作用。
package com.wolf.action;
import java.util.HashMap;
import java.util.Map;
public class demo {
public static void main(String args[]) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
System.out.println(Son.getInstance().getName());
System.out.println("我是谁");
}
}
class Son extends Father {
private String name = "儿子";
final String CLASS = "demo";
protected String getName() {
return this.query("aaa");
}
public static Son getInstance() throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
// 这里必须是全局路径 否则无法找到
return (Son) instance("com.wolf.action.Son");
}
}
class Father {
private static Map instance = new HashMap();
private String name = "父类";
protected void Fatcher() {
System.out.println("我是父类");
}
protected String query(String sql) {
return sql + "has been done";
}
public static Object instance(String objname)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException {
if (instance.get(objname) == null
|| !(instance.get(objname) instanceof Father)) {
instance.put(objname, Class.forName(objname).newInstance());
}
return instance.get(objname);
}
}