经典类:
class Foo:
新式类:
class Foo(object):
创建类的时候,继承object类,该类就是新式类!
这一切在python3之后发生了变化,python3里面创建的都是新式类
这两种类的区别:
新式类重定义的方法更多,当然这不是重点,重点是两种类在多继承状态下查找“方法”的规则不同。
经典类: 深度查找
显示类:广度查找
通过脚本来演示区别:
class Hero: def __init__(self,name): self.name = name def speciality(self): print("危难时刻,挺身而出")class Superhero: def where(self): print("出现在漫画中")class Rich: def __init__(self,name): self.name = name def speciality(self): print("有钱,花心")class Marvel(Superhero,Rich): def do_somethings(self): print("heal the world")
Marvel类的父类:Superhero,Rich
对象Iron_man执行方法speciality(),
新式类的查找过程:
Superhero——>Rich——>Hero
在Superhero中没有方法speciality(),所以执行Rich中的speciality()
>>> Iron_man = Marvel('Stark')
>>> Iron_man.speciality()有钱,花心同样的脚本,通过python2.7 运行
>>> Iron_man = Marvel('Stark')
>>> Iron_man.speciality()危难时刻,挺身而出