namespace PrivateDemo
{ class Student
{
private static Student s;
private Student()
{
Console.WriteLine("构造函数执行!!!");
}
public static Student GetStudent()
{
//s = new Student();//称为恶汉式
if (s == null) //称为懒汉式
s = new Student();
return s;
}
} /// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Student s=Student.GetStudent(); }
}
}
这段代码是以前考的,因为当初没在意私有构造这块也就没看,现在看看发觉却看懂了,请帮看一下这段代码的意义,要说明的问题是什么。
{ class Student
{
private static Student s;
private Student()
{
Console.WriteLine("构造函数执行!!!");
}
public static Student GetStudent()
{
//s = new Student();//称为恶汉式
if (s == null) //称为懒汉式
s = new Student();
return s;
}
} /// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Student s=Student.GetStudent(); }
}
}
这段代码是以前考的,因为当初没在意私有构造这块也就没看,现在看看发觉却看懂了,请帮看一下这段代码的意义,要说明的问题是什么。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货