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

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

什么是继承?

将父类的属性继承到子类中的方法称为继承。这是一个面向对象的概念。以下是继承的好处。

代码可重用性-我们不必一次又一次地编写相同的代码,我们可以继承子类中需要的属性。

它代表了父类和子类之间的现实关系。

它本质上是可传递的。如果子类从父类继承属性,则子类的所有其他子类也将继承父类的属性。

以下是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中有四种继承类型。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值