class Date:
def __init__(self, year, month, date):
self.year = year
self.month = month
self.date = date
@classmethod # 类方法,不用创建实例即可调用
def create(cls, dstr): # cls表示类本身, class
y, m, d = map(int, dstr.split('-')) # map(int, ['2000', '5', '4'])
dt = cls(y, m, d)
return dt
@staticmethod
def is_date_valid(dstr):
y, m, d = map(int, dstr.split('-'))
return 1 <= d <= 31 and 1 <= m <=12 and y < 4000
if __name__ == '__main__':
bith_date = Date(1995, 12, 3)
print(Date.is_date_valid('2000-5-4'))
day = Date.create('2000-5-4')
print(day)#######
#####################################
lass BearToy:
def __init__(self, nm, color, size):
self.name = nm
self.color = color # 绑定属性到实例
self.size = size
def sing(self):
print('lalala...')
def speak(self):
print('My name is %s' % self.name)
class NewBear(BearToy):
def __init__(self, nm, color, size, date):
# BearToy.__init__(self, nm, color, size)
super(NewBear, self).__init__(nm, color, size)
self.date = date
def run(self):
print('running...')
if __name__ == '__main__':
b1 = NewBear('venie', 'Brown', 'Small', '2018-07-20')
b1.sing()
b1.run()