如何在Python中訪問父類屬性?


在面向物件程式設計中,繼承允許我們建立繼承現有類屬性和方法的新類。這個強大的概念使我們能夠在程式中重用程式碼、模組化和擴充套件。在深入瞭解訪問父類屬性之前,讓我們快速回顧一下繼承。在Python中,當一個類繼承自另一個類時,它會獲取父類中定義的所有屬性和方法。這種機制允許我們建立專門的類,這些類繼承並擴充套件更通用的基類的功能。派生類也稱為子類,而被繼承的類稱為父類或基類。

示例

這是一個簡單的示例,用於說明繼承的概念:

class Parent:
   def __init__(self):
      self.parent_attribute = "I'm from the parent class"

class Child(Parent):
   def __init__(self):
      super().__init__()
      self.child_attribute = "I'm from the child class"

child = Child()
print(child.parent_attribute)  # Accessing parent class attribute
print(child.child_attribute)

輸出

I'm from the parent class
I'm from the child class

在這個例子中,我們有兩個類:Parent和Child。Child類使用語法`class Child(Parent)`繼承自Parent類。這意味著Child類繼承了Parent類中定義的所有屬性和方法。Child類還有一個稱為`child_attribute`的自己的屬性。

訪問父類屬性

要訪問Python中的父類屬性,您可以使用點表示法以及例項或類名。您選擇的方法取決於上下文和您的具體需求。讓我們探討訪問父類屬性的不同方法。

使用例項

如果您有子類的例項,您可以直接透過例項訪問父類屬性。該例項保留從父類繼承的所有屬性和方法,允許您輕鬆訪問它們。

示例

這是一個示例:

class Parent:
   def __init__(self):
      self.parent_attribute = "I'm from the parent class"

class Child(Parent):
   def __init__(self):
      super().__init__()
      self.child_attribute = "I'm from the child class"

child = Child()
print(child.parent_attribute)  # Accessing parent class attribute using instance

輸出

I'm from the parent class

在這個例子中,`child.parent_attribute`訪問在父類中定義的`parent_attribute`。透過例項訪問屬性,您可以檢索與該屬性關聯的值。

使用類名

除了透過例項訪問父類屬性之外,您還可以使用子類名訪問它們。當您沒有可用的例項但仍然想要直接訪問父類屬性時,此方法很有用。

示例

這是一個示例:

class Parent:
   parent_attribute = "I'm from the parent class"

class Child(Parent):
   child_attribute = "I'm from the child class"

print(Child.parent_attribute)  # Accessing parent class attribute using class name

輸出

I'm from the parent class

在這種情況下,`Child.parent_attribute`訪問在父類中定義的`parent_attribute`。透過使用類名,您可以直接訪問父類屬性,而無需例項。

訪問父類方法

繼承不僅允許我們訪問父類屬性,還允許我們訪問父類方法。當子類繼承自父類時,它會繼承父類中定義的所有方法。這意味著您可以使用例項或類名在子類中呼叫這些方法。

示例

這是一個示例:

class Parent:
   def parent_method(self):
      print("This is a method from the parent class")

class Child(Parent):
   def __init__(self):
      super().__init__()

child = Child()
child.parent_method()  # Accessing parent class method using instance
Child.parent_method()  # Accessing parent class method using class name

輸出

This is a method from the parent class
This is a method from the parent class

在這個例子中,Child類繼承了Parent類中的`parent_method`。我們可以使用Child類的例項(`child.parent_method()`)或直接使用類名(`Child.parent_method()`)來呼叫此方法。

覆蓋父類屬性

在某些情況下,您可能需要在子類中覆蓋父類屬性。覆蓋意味著為子類中的特定屬性提供不同的值或行為。透過在子類中重新定義屬性,您可以自定義其值,同時仍然可以使用前面討論的技術訪問父類屬性。

示例

這是一個示例:

class Parent:
   def __init__(self):
      self.shared_attribute = "I'm from the parent class"

class Child(Parent):
   def __init__(self):
      super().__init__()
      self.shared_attribute = "I'm from the child class"

child = Child()
print(child.shared_attribute)  # Accessing overridden attribute

輸出

I'm from the child class

在這個例子中,Parent和Child類都有一個名為`shared_attribute`的屬性。但是,在子類中,我們使用不同的值重新定義了該屬性。當我們使用子類的例項(`child.shared_attribute`)訪問該屬性時,我們會檢索在子類中定義的覆蓋值。

多重繼承

Python支援多重繼承,這意味著一個類可以繼承自多個父類。在使用多重繼承時,訪問父類屬性可能會變得更復雜。在這種情況下,您可能需要使用方法解析順序 (MRO) 或`super()`函式顯式指定要訪問的父類屬性。

示例

這是一個多重繼承和訪問父類屬性的示例:

class Parent1:
   def __init__(self):
      self.shared_attribute = "I'm from Parent1"

class Parent2:
   def __init__(self):
      self.shared_attribute = "I'm from Parent2"

class Child(Parent1, Parent2):
   def __init__(self):
      super().__init__()

child = Child()
print(child.shared_attribute)  # Accessing parent class attribute in multiple inheritance

輸出

I'm from Parent1

在這個例子中,Child類繼承自Parent1和Parent2類。當我們建立Child類的例項並訪問`shared_attribute`時,它會檢索在Parent1中定義的值。

受保護屬性和私有屬性

受保護屬性通常以單個下劃線 (_) 為字首,表示不應直接在類外部訪問它們,但子類仍然可以訪問它們。另一方面,私有屬性通常以雙下劃線 (__ ) 為字首,表示它們僅供類本身內部訪問。

示例

這是一個示例:

class Parent:
   def __init__(self):
      self._protected_attribute = "I'm a protected attribute"
      self.__private_attribute = "I'm a private attribute"

class Child(Parent):
   def __init__(self):
      super().__init__()

child = Child()
print(child._protected_attribute)   # Accessing protected attribute
print(child._Parent__private_attribute)   # Accessing private attribute

輸出

I'm a protected attribute
I'm a private attribute

在這個例子中,Parent類有一個受保護屬性`_protected_attribute`和一個私有屬性`__private_attribute`。子類Child仍然可以訪問這兩個屬性。但是,訪問私有屬性需要使用格式為`_ClassName__private_attribute`的名稱改編技術。

結論

繼承是一個強大的功能,它允許我們建立類層次結構並基於現有功能構建。透過訪問父類屬性,我們可以在程式中利用程式碼重用和模組化。

我們瞭解到,可以使用例項或類名來訪問父類屬性。透過實際示例,我們瞭解瞭如何使用子類的例項訪問父類屬性,以及如何使用類名直接訪問它們。

更新於:2023年8月16日

8K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.