首页 > 技术文章 > 10.02新式类与经典类的区别

liulvzhong 2017-10-02 16:19 原文


class Father(object): #有object就是新式类,没有object就是经典类,新式类修正了一 些bug。
def __int__(self):
self.Fname = "ffff"
print ("father.__init__")
def Func(self):
print ("father.Func")
def Bad(self):
print ("father.抽烟喝酒玩")
class Son(Father): ##可以继承多个类,用逗号隔开
def __init__(self):
self.Sname = "ssss"
print ("son.__init__")
super(Son,self).__init__() #Father加了object后,要用super才能继承,这样就是新式类了
def Bar(self):
print ("son.bar")
def Bad(self):
Father.Bad(self)
print ("son.赌博") #无论是经典还是新式类,继承后可以改变原Father的值或修改函数

s1 = Son()
print (s1.Bar())
print (s1.Func())
print (s1.Bad())

推荐阅读