0

我在 ubuntu 中使用 Geany 作为编辑器 python,并创建了一个包含该类的文件。

这是我的文件。时间1.py

class Time:

    def __int__(self):
        self.hour = 0
        self.minute = 0
        self.second = 0

    def printMilitary(self):
        print "%.2d:%.2d:%.2d" % \
          (self.hour, self.minute, self.second),

    def printStandard(self):

        standardtime = ""
        if self.hour == 0 or self.hour == 12:
            standardTime += "12:"
        else:
            standardTime += "%d:" % ( self.hour % 12 )
        standardTime += "%.2d:%.2d" % ( self.minute, self.second )

        if self.hour < 12:
            standardTime += " AM"
        else:
            standardTime += " PM"
        print standardTime,

所以我在 mytime.py 中调用它

from Time1 import Time

time1 = Time()

print "The attributes of time1 are: "
print "time1.hour:", time1.hour
print "time1.minute:", time1.minute
print "time1.second:", time1.second

之后我尝试运行这个脚本。但我得到了错误。这是错误

time1 的属性为: time1.hour: Traceback (last last call last): File "untitled.py", line 31, in print "time1.hour:", time1.hour AttributeError: Time instance has no attribute 'hour'

你能帮助我吗

4

1 回答 1

4

__int__需要__init__

于 2012-07-30T04:00:03.540 回答