Python编程语言很容易学习,可以使用过程性编程方法和面向对象编程方法。继承是面向对象编程中的一个这样的概念。代码可重用性是继承的强项,当我们在Python上工作时,它在很多应用程序中都有帮助。
什么是继承?
将父类的属性继承到子类的方法称为继承。它是一个面向对象的概念。以下是继承的好处。
1.代码可重用性-我们不必一遍又一遍地编写相同的代码,我们只需在子类中继承所需的属性即可。
2.它代表了父类和子类之间的真实关系。
3.它在性质上是及物性的。如果子类继承父类的属性,则子类的所有其他子类也将继承父类的属性。
下面是一个简单的Python继承示例。
class Parent():
def first(self):
print('first function')
class Child(Parent):
def second(self):
print('second function')
ob = Child()
ob.first()
ob.second()
Output:
first function
second function
在上面的程序中,您可以使用子类对象访问父类函数。
子类化
通过在子类的声明中提及父类名称来调用父类的构造函数称为子类。子类通过子类来标识其父类。
init()函数
每次使用类创建对象时,都会调用init()函数。当我们在父类中添加init()函数时,子类将不再能够继承父类的init()函数。该文件的类init()函数覆盖父类的init()函数。
class Parent:
def __init__(self , fname, fage):
self.firstname = fname
self.age = fage
def view(self):
print(self.firstname , self.age)
class Child(Parent):
def __init__(self , fname , fage):
Parent.__init__(self, fname, fage)
self.lastname = "python"
def view(self):
print("course name" , self.firstname ,"first came", self.age , " years ago." , self.lastname, " has courses to master python")
ob = Child("Python" , '28')
ob.view()
继承类型
根据涉及的子类和父类的数量,Python中有四种类型的继承。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B84TolCu-1625493226959)(images\118.png)]
单一继承
当子类仅继承单个父类时。
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print("this is function 2 ")
ob = Child()
ob.func1()
ob.func2()
多重继承
当一个子类从多个父类继承时。
class Parent:
def func1(self):
print("this is function 1")
class Parent2:
def func2(self):
print("this is function 2")
class Child(Parent , Parent2):
def func3(self):
print("this is function 3")
ob = Child()
ob.func1()
ob.func2()
ob.func3()
多级继承
当一个子类成为另一个子类的父类时。
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Child):
def func3("this is function 3")
ob = Child2()
ob.func1()
ob.func2()
ob.func3()
分层继承
分层继承涉及来自同一基类或父类的多个继承。
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Parent):
def func3(self):
print("this is function 3")
ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()
混合继承
混合继承涉及在单个程序中发生多个继承。
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child1(Parent):
def func3(self):
print(" this is function 3"):
class Child3(Parent , Child1):
def func4(self):
print(" this is function 4")
ob = Child3()
ob.func1()
Python Super()函数
超级函数允许我们从父类调用方法。
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
Super().func1()
print("this is function 2")
ob = Child()
ob.func2()
Python方法覆盖
方法重写您可以重写Python中的方法。请看下面的示例。
class Parent:
def func1(self):
print("this is parent function")
class Child(Parent):
def func1(self):
print("this is child function")
ob = Child()
ob.func1()
父类方法的功能是通过重写子类中的相同方法来更改的。
继承是面向对象编程中最重要的概念之一。它提供了可编码性、可读性和属性转换,这有助于优化和高效的代码构建。Python编程语言加载了继承等概念。在最近的市场上,庞大的Python应用需要越来越多的Python程序员。