如何限止类仅被实例化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();
}
}
}
}