【原】两个时间相加的运算符重载实现
要求:两个时间相加,得到的时间形如“xx时:xx分:xx秒”
--------------------------------------------------------------------------------------------------------------
解答:
首先编写时间处理类
时间类
1 /// <summary>
2 /// 时间类
3 /// </summary>
4 class Time
5 {
6 #region 域
7
8 private int _hours, _minutes, _seconds;
9
10 #endregion
11
12 #region 属性
13
14 /// <summary>
15 /// 秒
16 /// </summary>
17 public int Seconds
18 {
19 get { return _seconds; }
20 set { _seconds = value; }
21 }
22 /// <summary>
23 /// 分
24 /// </summary>
25 public int Minutes
26 {
27 get { return _minutes; }
28 set { _minutes = value; }
29 }
30 /// <summary>
31 /// 时
32 /// </summary>
33 public int Hours
34 {
35 get { return _hours; }
36 set { _hours = value; }
37 }
38
39 #endregion
40
41 #region 构造函数
42
43 /// <summary>
44 /// 无参构造函数
45 /// </summary>
46 public Time()
47 {
48 this._hours = 0;
49 this._minutes = 0;
50 this._seconds = 0;
51 }
52 /// <summary>
53 /// 有参构造函数
54 /// </summary>
55 /// <param name="hours">时</param>
56 /// <param name="minutes">分</param>
57 /// <param name="seconds">秒</param>
58 public Time(int hours,int minutes,int seconds)
59 {
60 this._hours = hours;
61 this._minutes = minutes;
62 this._seconds = seconds;
63 }
64
65 #endregion
66
67 #region 自定义函数
68
69 /// <summary>
70 /// 重载+函数
71 /// </summary>
72 /// <param name="time1">时间1</param>
73 /// <param name="time2">时间2</param>
74 /// <returns></returns>
75 public static Time operator +(Time time1,Time time2)
76 {
77 Time result = new Time();
78 result._seconds = time1._seconds + time2._seconds; // 计算秒
79 result._minutes = time1._minutes + time2._minutes + result._seconds / 60; // 计算分
80 result._hours = time1._hours + time2._hours + result._minutes / 60; // 计算时
81 result._seconds = result._seconds % 60; // 正确显示秒(超过60应该取余)
82 result._minutes = result._minutes % 60; // 正确显示分(超过60应该取余)
83 return result;
84 }
85
86 #endregion
87 }
2 /// 时间类
3 /// </summary>
4 class Time
5 {
6 #region 域
7
8 private int _hours, _minutes, _seconds;
9
10 #endregion
11
12 #region 属性
13
14 /// <summary>
15 /// 秒
16 /// </summary>
17 public int Seconds
18 {
19 get { return _seconds; }
20 set { _seconds = value; }
21 }
22 /// <summary>
23 /// 分
24 /// </summary>
25 public int Minutes
26 {
27 get { return _minutes; }
28 set { _minutes = value; }
29 }
30 /// <summary>
31 /// 时
32 /// </summary>
33 public int Hours
34 {
35 get { return _hours; }
36 set { _hours = value; }
37 }
38
39 #endregion
40
41 #region 构造函数
42
43 /// <summary>
44 /// 无参构造函数
45 /// </summary>
46 public Time()
47 {
48 this._hours = 0;
49 this._minutes = 0;
50 this._seconds = 0;
51 }
52 /// <summary>
53 /// 有参构造函数
54 /// </summary>
55 /// <param name="hours">时</param>
56 /// <param name="minutes">分</param>
57 /// <param name="seconds">秒</param>
58 public Time(int hours,int minutes,int seconds)
59 {
60 this._hours = hours;
61 this._minutes = minutes;
62 this._seconds = seconds;
63 }
64
65 #endregion
66
67 #region 自定义函数
68
69 /// <summary>
70 /// 重载+函数
71 /// </summary>
72 /// <param name="time1">时间1</param>
73 /// <param name="time2">时间2</param>
74 /// <returns></returns>
75 public static Time operator +(Time time1,Time time2)
76 {
77 Time result = new Time();
78 result._seconds = time1._seconds + time2._seconds; // 计算秒
79 result._minutes = time1._minutes + time2._minutes + result._seconds / 60; // 计算分
80 result._hours = time1._hours + time2._hours + result._minutes / 60; // 计算时
81 result._seconds = result._seconds % 60; // 正确显示秒(超过60应该取余)
82 result._minutes = result._minutes % 60; // 正确显示分(超过60应该取余)
83 return result;
84 }
85
86 #endregion
87 }
然后编写主函数相关代码
主函数
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 Time time1 = new Time(1, 42, 16); // 实例化对象1
6 Time time2 = new Time(2, 26, 52); // 实例化对象2
7 Time result = time1 + time2; // 重载+运算符以计算时间
8 Console.WriteLine("{0}时:{1}分:{2}秒", result.Hours, result.Minutes, result.Seconds);
9 Console.ReadLine();
10 }
11 }
2 {
3 static void Main(string[] args)
4 {
5 Time time1 = new Time(1, 42, 16); // 实例化对象1
6 Time time2 = new Time(2, 26, 52); // 实例化对象2
7 Time result = time1 + time2; // 重载+运算符以计算时间
8 Console.WriteLine("{0}时:{1}分:{2}秒", result.Hours, result.Minutes, result.Seconds);
9 Console.ReadLine();
10 }
11 }