當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python super()用法及代碼示例

眾所周知,Python是一種麵向對象的編程語言。因此,Python 遵循 OOP 的所有概念,其中一個概念就是繼承。

在使用繼承概念時,我們可以在繼承類或子類內部使用 super() 函數來引用父類。我們在子類中使用的 super() 函數返回超類的臨時創建對象,允許我們訪問子類中存在的所有方法。

super() 函數的好處:

以下是在子類中使用 super() 函數的好處:

  • 在使用 super() 函數時,我們不需要記住父類的類名。這是因為我們不必指定父類的名稱來訪問其中存在的方法。
  • 我們可以將 super() 函數與單繼承和多繼承一起使用。
  • Python 中的 super() 函數實現了代碼的可重用性和模塊化,因為我們不需要一次又一次地重寫整個函數。
  • Python 中的 super() 函數被稱為動態函數,眾所周知,Python 是一種動態類型的編程語言。

使用 super() 函數的限製:

以下是在 Python 程序中使用 super() 函數必須遵循的三個約束:

  • 參數在 super() 函數中給出,我們調用的函數中的參數應該匹配。
  • 我們正在使用的方法的每次出現都應該在我們使用它之後包含 super() 關鍵字。
  • 我們必須指定其中存在的類和方法,由 super() 函數引用。

現在,我們知道,我們可以在 Python 的兩種繼承類型中使用 super() 函數,即單繼承和多繼承。因此,我們將分別通過一個例子來學習在兩種類型的繼承中使用 super() 函數。

在 Python 中的單繼承中使用 super() 函數

在此示例中,我們將使用動物作為單繼承示例的參考。

cat 、馬、牛、狗等,都是動物綱的一部分。他們都有一些共同的特點:

  • 它們都是寵物動物。
  • 它們都有四隻腳和一條尾巴。
  • 作為動物綱的一部分,它們也是哺乳動物。

所以,我們可以說 cat 綱、馬綱和狗綱是動物綱的亞綱。這是單繼承的一個例子,因為所有的子類( cat 類、馬類和狗類)都隻從一個父類繼承,即動物類。現在,看看下麵的程序。

範例-

# 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.

說明:

在上麵的代碼中,我們將動物定義為父類,並從它繼承了家養、尾巴和腿和哺乳動物類。之後,我們定義了 cat 、馬和狗類,並在其中使用了 super 函數。借助這些類中的 super() 函數,我們訪問了 cat 、馬和狗類中動物界的方法。

在 Python 中的多重繼承中使用 super() 函數

在這個例子中,我們將使用一個父類,即哺乳動物類。然後,我們將從哺乳動物類繼承 'Can Fly' 和 'Can Swim' 類。

這些類將代表給定的哺乳動物是否會飛,是否會遊泳。之後我們將定義一個動物類,它將繼承 'Can Fly' 和 'Can Swim' 類,並返回給我們的動物是否具有定義的特征。

所以,我們可以看到我們這裏使用的動物類繼承自多個基類,因此,它是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

說明:

在上麵的代碼中,我們將哺乳動物定義為父類。之後,我們繼承了哺乳動物類的 can fly & can walk 類。借助super()函數,我們在animalia類中使用了can fly & can walk的方法。 Animalia 類繼承自會遊泳和會飛的類。





相關用法


注:本文由純淨天空篩選整理自 Python super() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。