【发布时间】:2017-05-09 03:29:53
【问题描述】:
如果例如小时大于 23、分钟大于 60 和秒大于 60,我正在尝试拒绝数据(转换为 0),但是我遇到了它不执行任何操作的问题。我不确定我只是没有正确初始化我的 get/set 方法还是什么。
这是我的代码:
class Clock(object):
def __init__(self, hour, minute, second):
self.__hour = hour
self.__minute = minute
self.__second = second
def setHour(self, hour):
self.__hour = hour
if self.__hour > 23:
self.__hour = 0
def getHour(self):
return self.__hour
def setMinute(self, minute):
self.__minute = minute
if self.__minute > 60:
self.__minute = 0
def getMinute(self):
return self.__minute
def setSecond(self, second):
self.__second = second
if self.__second > 60:
self.__second = 0
def getSecond(self):
return self.__second
def __str__(self):
if self.__hour > 11:
return 'The Time is {}:{}:{} PM'.format(self.__hour, self.__minute, self.__second)
else:
return 'The Time is {}:{}:{} AM'.format(self.__hour, self.__minute, self.__second)
stopwatch = Clock(0, 0, 0)
print(stopwatch)
watch = Clock(10, 30, 0)
print(watch)
wallclock = Clock(5, 66, 42)
print(wallclock)
【问题讨论】:
-
您的代码从不调用您的任何方法。
标签: python python-3.x class oop