标签:info 个数 return class 逻辑 man ret 运行 override
(来源:wiki)
public class Tree { private String color; public Tree(String color){ this.color =color; } @Override public String toString() { return color+" Tree"; } } public class TreeManager { private static final HashMap<String,Tree> TREE_MAP = new HashMap<>(); public static Tree getTree(String color){ Tree tree = (Tree)TREE_MAP.get(color); if(tree==null){ tree = new Tree(color); TREE_MAP.put(color,tree); } System.out.println(tree); return tree; } public static int countTrees(){ return TREE_MAP.size(); } } //测试 public class Client { public static void main(String[] args){ TreeManager.getTree("GREEN"); TreeManager.getTree("GREEN"); TreeManager.getTree("RED"); TreeManager.getTree("GREEN"); TreeManager.getTree("BLUE"); TreeManager.getTree("RED"); TreeManager.getTree("BLUE"); System.out.println("对象个数:"+TreeManager.countTrees()); } }
标签:info 个数 return class 逻辑 man ret 运行 override
原文地址:https://www.cnblogs.com/camcay/p/12377339.html