构造器调用构造器和const/readonly常量和static变量

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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值