#数字计时器类
from time import sleep#从time模块中引入sleep函数,使用sleep函数可以让程序休眠(推迟调用线程的运行)。
class Jishi(object):
'''数字时钟'''
def __init__(self,shi=0, fen=0 , miao=0):#默认参数!!!!!!!!!
self.shi = shi
self.fen = fen
self.miao = miao
def run(self):#逢60进一
self.miao += 1
if self.miao == 60:#if 缩进情况!!!秒-》分-》时
self.miao = 0
self.fen += 1
if self.fen == 60:
self.fen = 0
self.shi += 1
if self.shi == 24:
self.shi =0
def show(self):
'''显示时间'''
return '%02d:%02d:%02d' % (self.shi, self.fen, self.miao)
#格式符%02d中2d前面的前导符0表示输出数据时,若左边有多余位,则补0
def main():
clock= Jishi(23,59,58)
while True:
print(clock.show())
sleep(1)#间隔一秒输出
clock.run()#计算
if __name__=='__main__':
main()
# 定义?个类描述平?上的点并提供移动点和计算到另?个点距离的?法
from math import sqrt
class Miaodian():
def __init__(self, x=0, y=0): # 初始坐标
self.x = x
self.y = y
def move_to(self, x, y): # 点移动到目标位置
self.x = x
self.y = y
def dis_to(self, other): # other!!!!!!!!计算距离目标位置
a = self.x - other.x
b = self.y - other.y
return sqrt(a ** 2 + b ** 2)
def __str__(self):
return '(%d,%d)' % (self.x, self.y)
def main():
c1 = Miaodian(2, 3)
c2 = Miaodian()
print(c2)
print(c1.dis_to(c2))
c2.move_to(-1, 2)
print(c2)
if __name__=='__main__': #!!!!!!!
main()
|