正确调用类中的实例方法

时间:2017-09-14 09:50:28

标签: python class object methods

返回self.seconds.splitSeconds()的语句无法正常工作,因此请使用正确的语法指导我

class Time:
    def convertToSeconds(self):
      self.minutes = self.hours * 60 + self.minutes
      self.seconds = self.minutes * 60 + self.seconds
      return self.seconds

    def splitSeconds(seconds):
      self.hours = seconds // 3600
      self.minutes = (seconds % 3600) // 60
      self.seconds = seconds % 60
      return self

    def increment(self, seconds):
      self.seconds = self.convertToSeconds() + seconds
      return self.seconds.splitSeconds()

    def printTime(time):
      print(str(time.hours)+":"+str(time.minutes)+":"+str(time.seconds))

 time = Time()
 time.hours = 11
 time.minutes = 30
 time.seconds = 45

 seconds = 40

 time.increment(seconds)
 time.printTime()

2 个答案:

答案 0 :(得分:0)

我认为您需要将const imagePath = path.resolve(__dirname, 'public/files/pg363.jpg'); if (fs.existsSync(imagePath)) { watermark.embedWatermark(imagePath, options); // ... } 作为属性添加到self

splitSeconds

此外,您的退货声明应为

def splitSeconds(self, seconds)

您应该在实例上调用实例方法,而不是其他任何内容。

答案 1 :(得分:0)

您需要__init__初始化Time类,例如:

class Time:
    def __init__(self, hours, minutes, seconds):
        self.hours = hours
        self.minutes = minutes
        self.seconds = seconds

    def convertToSeconds(self):
      self.minutes = self.hours * 60 + self.minutes
      self.seconds = self.minutes * 60 + self.seconds
      return self.seconds

    def splitSeconds(self, seconds):
      self.hours = seconds // 3600
      self.minutes = (seconds % 3600) // 60
      self.seconds = seconds % 60


    def increment(self, seconds):
      self.seconds = self.convertToSeconds() + seconds
      return self.seconds

    def printTime(self):
      print(str(self.hours)+":"+str(self.minutes)+":"+str(self.seconds))

time = Time(11,30,45)

seconds = 40
time.increment(seconds)
time.printTime()