【问题标题】:Testing an Abstract Data Type测试抽象数据类型
【发布时间】:2015-06-01 22:32:15
【问题描述】:

我创建了一个名为“Time”的 ADT,需要创建一个新的 python 文件来测试它。以下是我所拥有的:

下面的代码使用 datetime 模块的时间能力来实现时间 ADT。

import datetime

class Time :
     # Creates an new time instance and initializes it with the given time.
    def __init__ (self, hours, minutes, seconds):
        self.hr = hours
        self.min = minutes
        self.sec = seconds

    def hour(self):
        # Returns the hour as an integer between 0 to 23.
        return int(self.hr)

    def min(self):
        # Returns the minute as an integer between 0 to 59.
        return int(self.min)

    def sec(self):
        # Returns the seconds as an integer between 0 to 59.
        return int(self.sec)

    def numSec(self, otherTime):
        # Returns the number of seconds elapsed between this time and the otherTime.
        return abs(self.sec - otherTime.seconds)

    def advanceBy(self, numSeconds):
        # Advances the time by the given number of seconds.
        if (numSeconds + self.sec) < 60:
             seconds = seconds + numSeconds
        if ((numSeconds / 60) + self.min) < 60:
                minutes = minutes + int(numSeconds / 60)
                seconds = seconds + numSeconds % 60
        else:
                hours = hours + int(numSeconds / 3600)
                minutes = minutes + (numSeconds % 3600)
                seconds = seconds + numSeconds % 60

    def isPM(self):
        # Returns a Boolean indicating if this time is at or after 12 o'clock noon.
        if self.hr > 11:
            return True
        else:
            return False

    def comparable(self, otherTime):
        # Compares this time to the otherTime to determine their logical ordering.
        if datetime.time(otherTime) > datetime.time(self.hr, self.min, self.sec):
            return otherTime + "is after" + (self.hr, self.min, self.sec)
        else:
            return otherTime + "is before"+ (self.hr, self.min, self.sec)

    def toString(self):
        # Returns "HH:MM:SS XX", in the 12-hr format where XX is either AM/PM.
        if self.hr > 11:
            return "%d:%d%d PM" % (hours, minutes, seconds)
        else:
            return "%d:%d%d AM" % (hours, minutes, seconds)

我创建了另一个文件,它应该导入这个模块(保存为 timeADT.py)并测试每个操作。但是,我不知道如何正确调用每个函数。我想将时间设置为(小时、分钟、秒),但是当我调用 timeADT.hour(5,12,49) 时,它给了我一个错误,并说我应该只有一个参数。

import timeADT

timeADT.hour(5,49,13)

我是 python 新手,对我从这里去哪里很困惑。任何有关如何创建此“测试”文件的帮助将不胜感激!

谢谢。

【问题讨论】:

标签: python class module abstract-data-type


【解决方案1】:

您应该先构造对象,然后再使用它。 试试这个:

from timeADT import Time
time = Time(5,49,13)
print time.hour, time.min, time.sec

【讨论】:

    【解决方案2】:

    但也有其他问题:

    def numSec(selfotherTime):
    

    self 后面缺少逗号

    def advanceBy(numSeconds):
    

    这里self, 完全不见了。

    【讨论】:

      猜你喜欢
      • 2010-09-16
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      相关资源
      最近更新 更多