有意思的登记式(乱入懒汉恶汉)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有意思的登记式(乱入懒汉恶汉)相关的知识,希望对你有一定的参考价值。
不多哔哔 只是一些小玩应,初学勿喷 希望能学到点东西
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;
}
}
以上是关于有意思的登记式(乱入懒汉恶汉)的主要内容,如果未能解决你的问题,请参考以下文章