如何重载处理时间的操作符“ - ”

时间:2011-03-19 18:49:26

标签: c# class console operator-overloading

让大脑痉挛试图为负面操作者弄明白。

来源:

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

namespace ClassTime
{


    class Program
    {

        class Time
        {
            public int h;
            private int m;
            private int s;

            private int getH()
            {
                return h;
            }

            private int getM()
            {
                return m;
            }

            private int getS()
            {
                return s;
            }

            public void getTime()
            {
                Console.WriteLine("{0} : {1} : {2}", h, m, s);
            }

            //set hours
            private void setH()
            {
                h = 0;
            }

            private void setH(int hours)
            {
                h = hours;
            }

            private void setH(Time obj)
            {
                h = obj.h;
            }

            //set minutes
            private void setM()
            {
                m = 0;
            }

            private void setM(int minutes)
            {
                while (minutes > 59)
                {
                    h++;
                    minutes -= 60;
                }
                m = minutes;
            }

            private void setM(Time obj)
            {
                m = obj.m;
            }

            //set seconds
            private void setS()
            {
                s = 0;
            }

            private void setS(int seconds)
            {
                while (seconds > 59)
                {
                    m++;
                    seconds -= 60;
                }
                s = seconds;
            }

            private void setS(Time obj)
            {
                s = obj.s;
            }

            //set Time
            private void setTime()
            {
                setH();
                setM();
                setS();
            }

            private void setTime(int hours, int minutes, int seconds)
            {
                setH(hours);
                setM(minutes);
                setS(seconds);
            }

            private void setTime(Time obj)
            {
                setH(obj);
                setM(obj);
                setS(obj);
            }

            //constructors
            public Time()
            {
                setTime();
            }

            public Time(int hours, int minutes, int seconds)
            {
                setTime(hours, minutes, seconds);
            }

            public Time(Time obj)
            {
                setTime(obj);
            }

            public static Time operator +(Time obj1, Time obj2)
            {
                Time temp = new Time();

                temp.h = obj1.h + obj2.h;

                temp.m = obj1.m + obj2.m;
                while (temp.m > 59)
                {
                    ++temp.h;
                    temp.m -= 60;
                }

                temp.s = obj1.s + obj2.s;
                while (temp.s > 59)
                {
                    ++temp.m;
                    temp.s -= 60;
                }
                return temp;               
            }

            public static Time operator -(Time obj1, Time obj2)
            {
                Time temp = new Time();

                temp.s = obj1.s - obj2.s;
                while (temp.s > 59)
                {
                    --temp.m;
                    temp.s -= 60;
                }

                temp.m = obj1.m - obj2.m;
                while (temp.m > 59)
                {
                    --temp.h;
                    temp.m -= 60;
                }

                temp.h = obj1.h - obj2.h;

                return temp;
            }
        }

        static void Main(string[] args)
        { 
            Time a = new Time(3, 180, 70);
            Time b = new Time(5, 30, 170);

            a.getTime();
            b.getTime();

            Time c = new Time();
            c = a + b;
            c.getTime();
        }
    }
}

我应该选择哪个座位?

1 个答案:

答案 0 :(得分:4)

我根本不会这样做 - 内置的DateTimeTimeSpan类已经支持这些运算符以及更多:

DateTime startTime;
DateTime endTime;
TimeSpan diff = endTime - startTime;

TimeSpan t1 = DateTime.Now.AddHours(-1).TimeOfDay;
TimeSpan t2 = DateTime.Now.TimeOfDay;
TimeSpan delta = t2 - t1;