C#定义时钟类(要求用操作符重载来完成)

[复制链接]
查看11 | 回复3 | 2009-12-4 13:46:52 | 显示全部楼层 |阅读模式
class TimeEx{//定义内部成员变量
private int mHour;
private int mMinute;
private int mSecond;//定义对外属性
public int Hour{
get
{
return mHour;
}
set
{
mHour=value;
}}//后面的minute,second 也一样...省略了..................}
回复

使用道具 举报

千问 | 2009-12-4 13:46:52 | 显示全部楼层
没测试过,不知有没bug。。。请自己补充。class Clock{
private int _Hour;
private int _Minute;
private int _Second;public int Hour{get{ return this._Hour;} set{ this._Hour = value;}}public int Minute{get{ return this._Minute;} set{ this._Minute = value;}}public int Second{get{ return this._Second;} set{ this._Second = value;}} public Clock(int hour, int minute, int second){if(hour11)throw new ArgumentException("hour should be between 0-11"); if(minute59)throw new ArgumentException("minute should be between 0-59"); if(second59)throw new ArgumentException("second should be between 0-59");this._Hour = hour; this._Minute = minute; this._Second = second;}public static Clock operator+ (Clock clock1, Clock clock2){Clock result;int hour, minute, second;second = clock1.Second + clock2.Second;minute = clock1.Minute + clock2.Minute;hour = clock1.Hour + clock2.Hour;
if(second >= 60){
minute++;
second -= 60;}if(minute >= 60){
hour++;
minute -= 60;}
if(hour >= 12){
hour -= 12;}
result = new Clock(hour, minute, second);return result;}public static Clock operator- (Clock clock1, Clock clock2){//类似上面,自己扩充吧。}
回复

使用道具 举报

千问 | 2009-12-4 13:46:52 | 显示全部楼层
哦..你想表达什么?..这个写个实体类就行了撒. 重复的方法重载就行.
回复

使用道具 举报

千问 | 2009-12-4 13:46:52 | 显示全部楼层
C#有现成的时间API啊,DateTime和TimeSpan有现成的时间API啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行