带有示例的Python继承:您需要了解的所有知识

Python编程语言易于学习,并且可用于过程式和面向对象的编程方法。继承是面向对象编程中的此类概念之一。代码可重用性是继承的长处,当我们使用Python时,它在许多应用程序中都有帮助。以下是本文讨论的概念:

  • 什么是继承?
  • 初始化功能
  • 继承类型
    • 单继承
    • 多重继承
    • 多级继承
    • 层次继承
    • 混合继承
  • Python super()函数
  • 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()

输出:

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 = "edureka"
     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中有四种继承类型。

单继承

当子类仅继承一个父类时。

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()

合智互联客户成功服务热线:400-1565-661

admin
admin管理员

上一篇:LiteOS之Lwm2m对接华为云流程分析(上)
下一篇:连老手也容易犯错的Zabbix SNMP该如何正确配置?

留言评论

暂无留言
取消
扫码支持