编程练习:定义一个表示时间的类Time
定义一个表示时间的类Time ,它提供下面操作:
a)Time(hours,minutes,seconds)创建一个对象;
b) t.hours(),t.minutes(),t.seconds()分别 返回时间对象t的小时,分钟和秒值;
c)为Time对象定义加法和减法操作
d)定义时间对象的等于和小于关系运算
代码示例:
class Time:def __init__(self,hours,minutes,seconds):self.hours = hoursself.minutes = minutesself.seconds = secondsdef hours(self):return self.hoursdef minutes(self):return self.minutesdef seconds(self):return self.secondsdef __add__(self,another):hours = ((self.hours*3600 + self.minutes*60 + self.seconds + another.hours*3600 + another.minutes*60 + another.seconds)//3600)minutes = ((self.hours*3600 + self.minutes*60 + self.seconds + another.hours*3600 + another.minutes*60 + another.seconds - 3600*hours)//60)seconds = (self.hours*3600 + self.minutes*60 + self.seconds + another.hours*3600 + another.minutes*60 + another.seconds - 60*minutes - 3600*hours)return Time(hours,minutes,seconds)def __sub__(self,another):hours = ((self.hours*3600 + self.minutes*60 + self.seconds - another.hours*3600 - another.minutes*60 - another.seconds)//3600)minutes = ((self.hours*3600 + self.minutes*60 + self.seconds - another.hours*3600 - another.minutes*60 - another.seconds - 3600*hours)//60)seconds = (self.hours*3600 + self.minutes*60 + self.seconds - another.hours*3600 - another.minutes*60 - another.seconds - 60*minutes - 3600*hours)return Time(hours,minutes,seconds)def __eq__(self,another):return self.hours*3600 + self.minutes*60 + self.seconds == another.hours*3600 + another.minutes*60 + another.secondsdef __lt__(self,another):return self.hours*3600 + self.minutes*60 + self.seconds < another.hours*3600 + another.minutes*60 + another.secondsdef print(self):print(self.hours,"h",self.minutes,"m",self.seconds,"s")t1 = Time(1,20,33)
t2 = Time(2,25,47)
t3 = t2 - t1
t4 = t2 + t1
t3.print()
t4.print()
编程练习:定义一个表示时间的类Time相关推荐
- java定义一个类计算圆的半径,C++编程:定义一个圆类要求属性为半径,操作为计算圆的周长和面积...,java编程:定义一个圆类,属性为半径,方法为对输入的半径计...
导航:网站首页 > C++编程:定义一个圆类要求属性为半径,操作为计算圆的周长和面积...,java编程:定义一个圆类,属性为半径,方法为对输入的半径计 C++编程:定义一个圆类要求属性为半径, ...
- 定义一个Teacher(教师)类,和一个Student(学生)类
定义一个Teacher(教师)类,和一个Student(学生)类,二者有一部分数据成员是相同的,例如num(号码),name(姓名),sex(性别).编写程序,将一个Student对象 转换为Teac ...
- java定义一个学生类cstudent_编写一个JAVA程序片断 定义一个表示学生的类student
编写一个JAVA程序片断定义一个表示学生的类student,包括域"学号","班级","姓名","性别","年 ...
- Java编写学生类student程序_编写一个JAVA程序片断定义一个表示学生的类student.docx...
编写一个JAVA程序片断 定义一个表示学生的类student,包括域"学号","班级","姓名","性别"," ...
- 实验3.2 定义一个简单的Computer类
题目 定义一个简单的Computer类,有数据成员芯片(cpu).内存(ram).光驱(cdrom)等等,有两个公有成员函数run.stop.cpu为CPU类的一个对象,ram为RAM类的一个对象,c ...
- 学号java_java ---- 定义一个表示学生的类 包括域“学号”、“班号”、“姓名”、“年龄”;...
1.编写一个JAVA类程序片段,定义一个表示学生的类student,包括域"学号"."班号"."姓名"."年龄":方法& ...
- 定义一个名为Cylinder类表示圆柱,它继承Circle类
这里写目录标题 题目描述: Circle类 运行结果 Cylinder类 运行结果 题目描述: (1)定义一个名为Circle的类表圆,其中含有double型的成员变量centerX.centerY表 ...
- 请定义一个交通工具(Vehicle)的类 其中有属性: 速度(speed) 体积(size)等等 方法移动(move())
一.题目 请定义一个交通工具(Vehicle)的类 其中有属性: 速度(speed) 体积(size)等等 方法移动(move()) 设置速度(setSpeed(int speed)) 加速speed ...
- 类的成员变量 请定义一个交通工具(Vehicle)的类其中有: 属性速度(speed)体积(size)等等 方法移动(move())设置速度(setSpeed(int speed))加速speed
/*类的成员变量 请定义一个交通工具(Vehicle)的类其中有:属性速度(speed)体积(size)等等 方法移动(move())设置速度(setSpeed(int speed))加速speedU ...
最新文章
- 一致性 Hash 算法的实际应用
- R语言可视化分面图、假设检验分组t检验、可视化单变量分组分面箱图(faceting boxplot)并添加p值(Make facet boxplot and add p-values)、添加抖动数据点
- php facade模,PHP 设计模式之外观模式 Facade
- 入门系列之在Ubuntu上安装Drone持续集成环境
- 科研神器推荐之那些年你用过的工具
- oracle动一下卡一下,记录一次Oracle很卡事件
- 执行jar包中的某个类
- 大连交通大学计算机网络作业,计算机网络题库(大连交通大学)chapter4
- caffe绘制训练过程的loss和accuracy曲线
- 多维数据查询效率分析(2)
- 百度地图JavaScript 选择 省份 显示 出省份的区域以及省份下的信息标点
- 跨平台iOS自动化测试工具——tidevice安装及使用
- WTL入门(五) 自定义控件
- 个人网站有赞云支付接口申请方法
- PUN 2 菜鸟养成记 2主服务
- 断舍离:通向自由的26个丢弃
- XSS网站漏洞如何修复 大牛支招让您网站更安全
- 在数据库创建时创建OMF(Oracle Managed Files,Oracle管理的文件)
- JS JSP HTML CSS概念
- 产品经理也能动手实践的AI(四)- 多标签识别,图像分割