using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SingleCase
{
class Program
{
static void Main(string[] args)
{
Case A1 = Case.StarCase();
A1.Setexc = 100;
for (int i = 0; i < 5; i++)
{
A1 = Case.StarCase();
A1.Setexc++;
}
Console.WriteLine(A1.Setexc);
Console.ReadLine();
}
}
class Case
{
private static Case Case1 = null;
private int exc;
public static Case StarCase()
{
if (Case1 == null)
{
Case1 = new Case();
return Case1;
}
return Case1;
}
public int Setexc { get { return exc; } set { exc = value; } }
}
}
此时Case类仅创建一次,如果已创建就返回已创建的类对象。