Singleton
单例模式也就是保证一个类只有一个实例的一种实现方法,
写法一:
class Program
{
static void Main(string[] args)
{
Earth person = Earth.GetEarth();
person.Population();
}
}
class Earth
{
private static Earth person = new Earth();
private Earth() { }
public static Earth GetEarth()
{
return person;
}
public string Population();
}
写法二:
class Peson
{
private Peson()
{
}
private static Peson p = new Peson();
public static Peson Get()
{
return p;
}
}
class Program
{
static void Main(string[] args)
{
Peson p = Peson.Get();
}
}