Files
2025-09-30 16:49:43 +08:00

39 lines
1.4 KiB
C#

namespace Lesson7_静态成员练习
{
//一个类对象,在整个应用程序的生命周期中,有且只有一个该对象的存在
//不能在外部实例化,直接通过该类名就能得到位的的对象
class test
{
private static test t = new test();//2.new一个对象,就可以用test.t来的到这个类对象
//这样就有且只有一个该对象的存在
//4.private来让外部不能设置null
public int testInt = 10;
public static test T//5.写一个成员属性来定义设置和取值的方法
//这样就可以只让读,不让写
{
get
{
return t; //让t来包裹test.T,那么test.T.testInt和t.testInt就是相同的
}
set
{
Console.WriteLine("你可不能随便动");
}
}
public test()//1.默认无参构造函数定义为private
//这样不能在外部实例化 即在类外部test t = new test();
//3.但是虽然不能new一个对象,可以在外部设置为null
{
}
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine(test.T.testInt);
}
}
}