#类的实列方法至少带一个参数self,第一个参数为self
class mydata:
__months=[0,31,28,31,30,31,30,31,31,30,31,30,31] #私有的
def __init__(self,y,m,d):
if y<0:
raise Exception("无效年份")
if m<1 or m>12:
raise Exception("无效月份")
if y%400==0 or y%4==0 and y%100!=0:
mydata.__months[2]=29
else:
mydata.__months[2]=28
if d<1 or d>mydata.__months[m]:
raise Exception("无效日期")
self.year=y
self.month=m
self.day=d
def show(self):#实列的方法
print("%04d-%02d-%02d" %(self.year,self.month,self.day))
try:
d=mydata(2019,12,27)
d.show()
except Exception as e:
print(e)