继承
一个类共享另一个类的数据结构和方法的机制称为继承。
起始类称为基类、超类、父类,而继承类称为派生类、子类,继承类是对被继承类的扩展。
概念 : 子类拥有父类的所有方法和属性
目的 : 实现代码的重用,相同的代码不需要重复的编写。
分类 : 单继承,多继承。
举例☞动物:吃,睡,运动。
狗:吃,睡,运动,汪汪叫。
中华田园犬:吃,睡,运动,汪汪叫,看家护院。
class Animal:
def eat(self):
print('吃')
def sleep(self):
print('睡')
def move(self):
print('运动')
class Dog(Animal):
def wangwang(self):
print('汪汪叫')
class Chinese_rural_dog(Dog):
def guard(self):
print('看家护院')
BigYellow=Dog() # 创建狗类的对象为大黄狗
BigYellow.eat()
BigYellow.sleep()
BigYellow.move()
BigYellow.wangwang()
ShibaLnu=Chinese_rural_dog() # 创建中华田园犬类的对象为柴犬
ShibaLnu.eat()
ShibaLnu.sleep()
ShibaLnu.move()
ShibaLnu.wangwang()
ShibaLnu.guard()