不多哔哔 只是一些小玩应,初学勿喷 希望能学到点东西
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class AloneDemo {
public static void main(String[] args)
{
CheckDemo cd1 = CheckDemo.getInstance(3);
//sopln(cd1.getCount());
CheckDemo cd2 = CheckDemo.getInstance(3);
//sopln(cd2.getCount());
CheckDemo cd4 = CheckDemo.getInstance(3);
//sopln(cd4.getCount());
CheckDemo cd5 = CheckDemo.getInstance(3);
//sopln(cd5.getCount());
CheckDemo cd3 = CheckDemo.getInstance(3);
//sopln(cd3.getCount());
CheckDemo cd6 = CheckDemo.getInstance(3);
//sopln(cd6.getCount());
}
public static void sopln(Object obj)
{
System.out.println(obj.toString());
}
}
class HungryMan
{
private HungryMan hm = new HungryMan();
private HungryMan()
{
}
public HungryMan getInstance()
{
return hm;
}
}
class LazyMan
{
private LazyMan lm;
private LazyMan()
{
}
public LazyMan getInstance()
{
if(lm == null)
lm = new LazyMan();
return lm;
}
}
class CheckDemo
{
private static Map<Integer,CheckDemo> m = new HashMap();
static int count = -1;
private CheckDemo()
{
sopln("--"+count);
}
//输入最多能创建的实例,
public static CheckDemo getInstance(int i)
{
if(++count<i)
{
m.put(count, new CheckDemo());
int ran = (int)(Math.random()*count);
sopln(ran);
return m.get(ran);
// return m.get((int)(Math.random()*count));
}
else
{
int ran = (int)(Math.random()*i);
sopln(ran);
return m.get(ran);
// return m.get((int)(Math.random()*i));
}
}
public static void sopln(Object obj)
{
System.out.println(obj.toString());
}
public int getCount()
{
return count;
}
}
原文地址:http://13215514.blog.51cto.com/13205514/1956242