小结 代码 第二篇(共三篇)

2010-03-26  杨群 

以下是我编写的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 public class Time2
    {
        public int hour;
        public int minute;
        public int second;
        //Time2 constructor initializes instrance variables to zero to set default time to midnight

        public Time2()
        {
            SetTime(0, 0, 0);
        }

        public Time2(int hour)
        {
            SetTime(hour, 0, 0);
        }

        public Time2(int hour, int minute)
        {
            SetTime(hour, minute, 0);
        }
        public Time2(int hour, int minute, int second)
        {
            SetTime(hour, minute, second);
        }

        public Time2(Time2 time)
        {
            SetTime(time.hour, time.minute, time.second);

        }
        public void SetTime(int hourValue, int minuteValue, int secondValue)
        {
            hour = (hourValue >= 0 && hourValue < 24) ? hourValue : 0;
            minute = (minuteValue >= 0 && minuteValue < 60) ? minuteValue : 0;
            second = (secondValue >= 0 && secondValue < 60) ? secondValue : 0;
        }

        public string ToUniversalString()
        {
            return String.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second);
        }

        public string ToStandardString()
        {
            return String.Format("{0}:{1:D2}:{2:D2}:{3}", ((hour == 12 || hour == 0) ? 12 : hour % 12), minute, second, (hour < 12 ? "AM" : "PM"));
        }
    }

 

 

 

调用的部分

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace TimeProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Time2 time1, time2, time3, time4, time5, time6;

            time1 = new Time2();//00:00:00
            time2 = new Time2(2);//02:00:00
            time3 = new Time2(21, 34);//21:34:00

            time4 = new Time2(12, 25, 42);//12:25:42

            time5 = new Time2(27, 74, 99);//00:00:00
            time6 = new Time2(time4); //12:25:42

            String output = "显示:" + "\ntime1:所有的参数都为默认" + "\n\t" + time1.ToUniversalString() + "\n\t" + time1.ToStandardString();

            output += "\ntime2:hour 指定的;minute and " + "second 为默认的" + "\n\t" + time2.ToUniversalString() + "\n\t" + time2.ToStandardString();

            output += "\ntime3:hour 和 minute 指定的;minute and " + "second 为默认的" + "\n\t" + time3.ToUniversalString() + "\n\t" + time3.ToStandardString();
          
            output += "\ntime4:hour ,minute,和 second 指定的" + "\n\t" + time4.ToUniversalString() + "\n\t" + time4.ToStandardString();

            output += "\ntime5:所有的参数都为指定的且为不正确的" + "\n\t" + time5.ToUniversalString() + "\n\t" + time5.ToStandardString();

            output += "\ntime6:Time2 object time4 指定的 " + "\n\t" + time6.ToUniversalString() + "\n\t" + time6.ToStandardString();

            System.Windows.Forms.MessageBox.Show(output, "演示重载构造函数");

        }//结束 Main
    }//结束 Program
}

263°/2632 人阅读/0 条评论 发表评论

登录 后发表评论