python中子类使用父类属性
# -*- coding:utf-8 -*-
# file_name: python_class_inheritance_example.py
class Animal(object):#这里的object非常关键
def __init__(self, name):
self.name = name
def saySomething(self):
print("I am " + self.name)
class Dog(Animal):
def __init__(self, name):
Animal.__init__(self, name)
def saySomething(self):
print ("I am " + self.name + ", and I can bark")
def saySomething_a(self):
super(Dog, self).saySomething()
if __name__ == "__main__":
dog = Dog("Blake")
dog.saySomething()
dog.saySomething_a()