眾所周知,Python 是一種面向對象的編程語言。因此,Python 遵循了 OOPs 的所有概念,其中一個概念就是繼承。
在使用繼承概念時,我們可以在繼承類或子類中使用 super()
函數來引用父類。我們在子類中使用的 super()
函數返回超類的一個臨時創建的對象,它允許我們訪問它在子類中的所有方法。
super()功能的優勢:
以下是在子類中使用 super()
函數的好處:
- 使用
super()
函數時,我們不需要記住父類的名字。這是因為我們不必指定父類的名稱來訪問其中存在的方法。 - 我們可以使用 super() 函數,具有單遺傳和多遺傳。
- Python 中的
super()
函數實現了代碼的可重用性和模塊化,因為我們不需要一次又一次地重寫整個函數。 - Python 中的
super()
函數被稱為動態函數,因為我們都知道 Python 是一種動態類型的編程語言。
使用 super()
函數的限制:
下面是在 Python 程序中使用 super()
函數必須遵循的三個約束:
- 參數在
super()
函數中給出,我們調用的函數中的參數應該匹配。 - 我們正在使用的方法的每次出現都應該在我們使用之後包含 super()關鍵字。
- 我們必須指定其中存在的類和方法,它們由
super()
函數引用。
現在,正如我們所知,我們可以在 Python 中的兩種繼承類型中使用 super()
函數,即單一繼承和多重繼承。因此,我們將學習如何在兩種類型的繼承中分別使用 super()
函數,並給出一個例子。
在 Python 的單繼承中使用 super()
函數
在這個例子中,我們將使用動物作為單遺傳例子的參考。
貓、馬、牛、狗等。,都是動物類的一部分。它們都有一些共同的特點:
- 它們都是寵物。
- 它們都有四條腿和一條尾巴。
- 作為動物界的一部分,它們也都是哺乳動物。
所以,我們可以說類貓、類馬、和類狗是動物界的子類。這是一個單一繼承的例子,因為所有的子類(貓類、馬類和狗類)都只從一個單一的父類繼承,即 animalia 類。現在,看看下面的程序。
示例-
# Define parent class animalia
class Animalia:
# define construcors for parent animalia class
def __init__(self):
self.Legs = 4
self.adomestic = True
self.atail = True
self.amammals = True
# define mammal class as child class
def aMammal(self):
if self.amammals:
print("The given animal is a mammal type .")
# define domestic class as child class
def aDomestic(self):
if self.adomestic:
print("The given animal is a domestic animal type.")
# define dog class
class Dog(Animalia):
def __init__(self):
super().__init__() # using super() function to access class methods
def isMammal(self):
super().aMammal() # using mammal class
# define cat class
class Cat(Animalia):
def __init__(self):
super().__init__()
def isMammal(self):
super().aDomestic() # using domestic class
# define horse class
class Horse(Animalia):
def __init__(self):
super().__init__()
def TailandLegs(self): # using tail and legs class
if self.atail and self.Legs == 4:
print("The given animal has four legs and a tail")
# Taking the driver's code for defined classes
Tommy = Dog()
Tommy.aMammal()
Tom = Cat()
Tom.aDomestic()
Burno = Horse()
Burno.TailandLegs()
輸出:
The given animal is a mammal type.
The given animal is a domestic animal type.
The given animal has four legs and a tail.
說明:
在上面的代碼中,我們已經將 animalia 定義為父類,並從其繼承了家養類、尾腿類和哺乳動物類。之後,我們定義了貓、馬和狗類,並在其中使用了超級函數。藉助這些類中的 super()
函數,我們可以訪問貓、馬和狗類中 animalia 類的方法。
在 Python 的多個繼承中使用 super()
函數
在這個例子中,我們將使用一個父類,即哺乳動物類。然後,我們將從哺乳動物類繼承「會飛」和「會游泳」類。
這些類別將代表給定的哺乳動物會不會飛,會不會游泳。之後,我們將定義一個動物類,它將繼承「會飛」和「會游」類,並返回給我們給定的動物是否具有定義的特徵。
因此,我們可以看到我們在這裡使用的動物類是從多個基類繼承的,因此,它是 Python 中多個繼承的一個例子。現在,看看下面的程序。
例
# Define Mammal class as parent class
class aMammals():
def __init__(self, name):
print(name, "Is a mammal of animalia class")
# define can fly as child class
class FlyCapable(aMammals):
def __init__(self, FlyCapable_name):
print(FlyCapable_name, "is not capable of flying")
# Calling Parent class Constructor
super().__init__(FlyCapable_name)
# define can swim as child class
class SwimCapable(aMammals):
def __init__(self, SwimCapable_name):
print(SwimCapable_name, "is not capable of swimming")
super().__init__(SwimCapable_name)
# Inherit animalia class from both fly and swim class
class animalia(FlyCapable, SwimCapable):
def __init__(self, name):
super().__init__(name) # using super() function
# Taking driver Code for animalia class
Burno = animalia("Cat")
輸出:
Cat is not capable of flying
Cat is not capable of swimming
Cat Is a mammal of animalia class
說明:
在上面的代碼中,我們將哺乳動物定義為父類。之後,我們從哺乳動物班繼承了會飛和會游泳的課程。藉助 super()
函數,我們在 animalia 類中使用了既能飛又能游的方法。animalia 級繼承了會游泳和會飛的級別。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248834.html