日期:2014-05-19  浏览次数:20404 次

如何限止类仅被实例化N次?
如何限止类仅被实例化N次?

eg:

User   oUser   =   new   User();

.....

要求只能被实例化5次

------解决方案--------------------
public class User
{
static count=0;
private User(){}
static key = new Object();
public User GetInstance()
{
if(count> =0)
{
throw new Exception();
}
else
{
lock(key)
{
count++;
return new User();
}
}
}
}