继承被定义为将基类的属性继承给子类的机制。下面我们来看看Python 中的继承类。
继承类型取决于所涉及的子类和父类的数量。Python中有四种继承类型:
单一继承使派生类能够从单个父类继承属性,从而实现代码可重用性以及向现有代码添加新功能。
例子:
# Python program to demonstrate
# single inheritance
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class
class Child(Parent):
def func2(self):
print("This function is in child class.")
# Driver's code
object = Child()
object.func1()
object.func2()
输出:
This function is in parent class.
This function is in child class.
当一个类可以从多个基类派生时,这种类型的继承称为多重继承。在多重继承中,基类的所有功能都被继承到派生类中。
例子:
# Python program to demonstrate
# multiple inheritance
# Base class1
class Mother:
mothername = ""
def mother(self):
print(self.mothername)
# Base class2
class Father:
fathername = ""
def father(self):
print(self.fathername)
# Derived class
class Son(Mother, Father):
def parents(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
# Driver's code
s1 = Son()
s1.fathername = "RAM"
s1.mothername = "SITA"
s1.parents()
输出:
Father : RAM
Mother : SITA
在多级继承中,基类和派生类的特征被进一步继承到新的派生类中。这类似于代表孩子和祖父的关系。
例子:
# Python program to demonstrate
# multilevel inheritance
# Base class
class Grandfather:
def __init__(self, grandfathername):
self.grandfathername = grandfathername
# Intermediate class
class Father(Grandfather):
def __init__(self, fathername, grandfathername):
self.fathername = fathername
# invoking constructor of Grandfather class
Grandfather.__init__(self, grandfathername)
# Derived class
class Son(Father):
def __init__(self, sonname, fathername, grandfathername):
self.sonname = sonname
# invoking constructor of Father class
Father.__init__(self, fathername, grandfathername)
def print_name(self):
print('Grandfather name :', self.grandfathername)
print("Father name :", self.fathername)
print("Son name :", self.sonname)
# Driver code
s1 = Son('Prince', 'Rampal', 'Lal mani')
print(s1.grandfathername)
s1.print_name()
输出:
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
当从单一基类创建多个派生类时,这种类型的继承称为层次继承。在此程序中,我们有一个父(基)类和两个子(派生)类。
例子:
# Python program to demonstrate
# Hierarchical inheritance
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class1
class Child1(Parent):
def func2(self):
print("This function is in child 1.")
# Derivied class2
class Child2(Parent):
def func3(self):
print("This function is in child 2.")
# Driver's code
object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()
输出:
This function is in parent class.
This function is in child 1.
This function is in parent class.
This function is in child 2.
由多种继承组成的继承称为混合继承。
例子:
# Python program to demonstrate
# hybrid inheritance
class School:
def func1(self):
print("This function is in school.")
class Student1(School):
def func2(self):
print("This function is in student 1. ")
class Student2(School):
def func3(self):
print("This function is in student 2.")
class Student3(Student1, School):
def func4(self):
print("This function is in student 3.")
# Driver's code
object = Student3()
object.func1()
object.func2()
输出:
This function is in school.
This function is in student 1.
原文链接:codingdict.net