Python3 类方法、静态方法新解

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/haeasringnar/article/details/100061487
class Date:
  def __init__(self, year, month, day):
    self.year = year
    self.month = month
    self.day = day

  # 实例方法
  def tomorrow(self):
    self.day +=1

  def __str__(self):
    return '{}/{}/{}'.format(self.year,self.month,self.day)

  # 静态方法
  @staticmethod
  def format_date_str(date_str):
    year, month, day = tuple(date_str.split('-'))
    return Date(int(year),int(month),int(day))

  # 类方法
  # 这里的 cls 实际就是类本身,它将自己本身返回,不需要我们写返回的类名,更好一些
  @classmethod
  def format_str(cls, date_str):
    year, month, day = tuple(date_str.split('-'))
    return cls(int(year),int(month),int(day))

if __name__ == "__main__":
  new = Date(2018,12,12)
  print(new)
  new.tomorrow()
  print(new)

  # 现在我们想输入一个日期字符串需要怎么做呢?
  date_str = '2018-12-30'
  year, month, day = tuple(date_str.split('-')) # 这里利用了tuple的拆包属性,将分开的列表分别赋给变量
  new = Date(year,month,day)
  print(new)
  # 如果有静态方法,就会更加简单了
  new = Date.format_date_str('2019-12-01')
  print(new)
  # 但是静态方法还要将类的名称返回,那有没有更好的方法呢
  # 那就是类方法,类方法的原理就是 将输入的参数处理后 通过类方法返回一个实例对像,静态方法也是如此,但静态方法可以不返回实例 而返回其他的
  new = Date.format_str('2019-9-01')
  print(new)
  # 那么问题来了?什么使用用静态方法,什么时候使用类方法呢?
  # 原则上是:当需要返回实例时使用类方法,不需要返回实例对象时 直接使用静态方法就好了,
  # 例如我们做验证日期字符串是否合法的时候没必要返回实例,那就使用 静态方法就可以了

猜你喜欢

转载自blog.csdn.net/haeasringnar/article/details/100061487