using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyTryParse { class DateConstructors { static void Main(string[] args) { try { Date mydate = new Date(2001, 8, 29); Console.WriteLine("Day of year = " + mydate.DayOfYear); Console.ReadKey(); } catch(Exception exc) { Console.WriteLine(exc); } } } class Date { //声明字段 int year; int month; int day; static int[] MonthDays = new int[] { 0,31,59,90,120,151,181,212,243,273,304,334}; //构造函数 public Date() { Year = 1600; Month = 1; Day = 1; } public Date(int year, int month, int day) { if((month == 2 && IsLeapYear(year) && day >29) || (month == 2 && !IsLeapYear(year) && day > 28) || ((month == 4 || month ==6 || month == 9 || month == 11) && day > 30)) { throw new ArgumentOutOfRangeException("Day"); } else { Year = year; Month = month; Day = day; } } //属性 public int Year { set { if (value < 1600) throw new ArgumentOutOfRangeException("Year"); else year = value; } get { return year; } } public int Month { set { if (value < 1 || value > 12) throw new ArgumentOutOfRangeException("Month"); else month = value; } get { return month; } } public int Day { set { if (value < 1 || value > 31) throw new ArgumentOutOfRangeException("Day"); else day = value; } get { return day; } } public int DayOfYear { get { return MonthDays[month - 1] + day + (month > 2 && IsLeapYear(year) ? 1 : 0); } } //方法 public static bool IsLeapYear(int year) { return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); } } }
|