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()
posted @ 2020-08-21 12:55  邱明成  阅读(2374)  评论(0编辑  收藏  举报