python 面向对象的三大特征之 继承


#继承
  #object 基类,是python定义的所有类的父类
  #经典类:不继承object的类称作经典类
  #新式类:继承object的类称作新式类
  #python 3.x统一为新式类
  #经典类是类对象,新式类是类型对象

 

#经典类的继承是按照继承的顺序进行继承的

#新式类是按照修改的优先级来继承,越后修改优先级就越高。

 

class Parent:                                 #定义一个父类
  def __init__(self):
    self.age = 0
  def sing(self):
    print("sing a song 2")

class Child(Parent):        #定义一个子类
  def sleep(self):
    print("ZZZzzzz....")
  def sing(self):                              #重写,保留父类属性的同时有自己的属性
    Parent.sing(self)
    print(self)
    print("Sing a Song 1")

father = Parent()
son = Child()
print(son.age)
son.sing() #---> Child.sing(son)

"""

0
sing a song 2
<__main__.Child object at 0x00000000007ECEF0>
Sing a Song 1

"""

 

posted @ 2016-10-03 11:20  xuanhui  阅读(179)  评论(0编辑  收藏  举报