如何重载运算符“-”处理时间的
尝试找出负运算符时,大脑会痉挛。
资料来源:
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();
}
}
}
我应该坐哪个座位?
Get brain cramps tryig to figure it out for the negative operator.
Source:
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();
}
}
}
which seat should i take?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我根本不会这样做 - 内置
DateTime< /code>
和
TimeSpan
< /a> 类已经支持这些运算符以及更多:I wouldn't do this at all - the built in
DateTime
andTimeSpan
classes already support these operators and more: