使用枚举和结构输出日期

转角撞倒猪 原文 使用枚举和结构输出日期

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
      
    namespace structType  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Date defaultDate = new Date();  // 使用默认构造器  
                Console.WriteLine("使用结构的默认构造器输出:{0}", defaultDate);   
                    // 实际上,输出时调用了重写的 ToString 方法  
                Console.WriteLine("使用结构的默认构造器输出:{0}", defaultDate.ToString());    
                    // 和上句效果一样,上句省略了 ToString 方法  
      
                Date weddingAnniversary = new Date(2010, Month.July, 4);  
                Console.WriteLine("使用自定义的构造器输出:{0}", weddingAnniversary);    
                    // 调用了重写的 ToString 方法  
            }  
        }  
      
        struct Date  
        {  
            private int year;       // 0 无效  
            private Month month;    // 0 为枚举类型中的 January  
            private int day;        // 0 无效  
      
            public Date(int ccyy, Month mm, int dd)  
            {  
                this.year = ccyy - 1900;  
                this.month = mm;  
                this.day = dd - 1;  
            }  
      
            public override string ToString()   // 重写 ToString 方法  
            {  
                string data = string.Format("{0} {1} {2}",  this.month,   
                                                            this.day + 1,   
                                                            this.year + 1900);  
                return data;  
            }  
        }  
      
        enum Month  // enum 类型  
        {   
            January, Feburary, March,  
            April, May, June,  
            July, August, September,  
            October, November, December  
        }  
    }  

运行后结果如下所示:

原文地址:https://www.cnblogs.com/arxive/p/5947902.html