Python 是否支持多重继承
是的,Python支持多重继承。和C++一样,一个类可以从多个基类派生出来。这被称为多重继承。
在多重继承中,所有基类的特征都被继承到派生类中。让我们看一下语法−
语法
Class Base1:
Body of the class
Class Base2:
Body of the class
Class Base3:
Body of the class
.
.
.
Class BaseN:
Body of the class
Class Derived(Base1, Base2, Base3, … , BaseN):
Body of the class
派生类继承自Base1、Base2和Base3类。
示例
在下面的示例中,Bird类继承了Animal类。
- Animal是父类,也被称为超类或基类。
-
Bird是子类,也被称为子类或派生类。
issubclass方法确保Bird是Animal类的子类。
class Animal:
def eat(self):
print("It eats insects.")
def sleep(self):
print("It sleeps in the night.")
class Bird(Animal):
def fly(self):
print("It flies in the sky.")
def sing(self):
print("It sings a song.")
print(issubclass(Bird, Animal))
Koyal= Bird()
print(isinstance(Koyal, Bird))
Koyal.eat()
Koyal.sleep()
Koyal.fly()
Koyal.sing()
输出
True
It eats insects.
It sleeps in the night.
It flies in the sky.
It sings a song.
True