static 静态变量=类中共享变量
using System;
namespace 构造器调用构造器
{
class Date//日期类 构造器调用构造器
{
private int year;
private int month;
private int day;
public Date(int _year, int _month, int _day)
{
year = _year;
month = _month;
day = _day;
}
public Date():
this(2000,1,2)
{
}
public void Print()
{
Console.WriteLine("{0}/{1}/{2}",year ,month ,day );
}
}
class Test //const和readonly常量
{
public const int c = 55;//const为常量,不可被更改
public readonly int ro; //只读常量,只能在类中被赋值
public Test(int n)
{
ro = n;
}
}
class Test2 //static静态变量 其实=share共享,就是把这个类中的变量共享给大家共用
{
public static int n;
}
class Program
{
static void Main(string[] args)
{
#region static变量调用
Test2 tc1 = new Test2();
Test2 tc2 = new Test2();
Test2.n = 3; //类名.成员名 因为被类的所有成员共享
Test2.n = 5;
Console.WriteLine(Test2.n);
#endregion
#region constl和 readonly 常量调用
Test t = new Test(1);
Console.WriteLine(t.ro); //readonly常量调用格式:对象名.成员名
Test t2 = new Test(2);
Console.WriteLine(t2.ro); //readonly常量调用格式:对象名.成员名
Console.WriteLine(Test.c); //const常量调用格式:类名.成员名
#endregion
#region 日期
Date d = new Date(2020, 1, 1);
Date d2 = new Date();
d.Print();
d2.Print();
#endregion
Console.ReadLine();
}
}
}