繼承
繼承使用圓括號(父類別)。
語法:
class 子類別(父類別):
class Child(Parent):
以上是 Child 類別繼承 Parent 類別,要繼承的類別寫在圓括號()中。
繼承父類別的屬性
Child 子類別透過繼承,擁有父類別的屬性name,所以可以使用self.name。
1
2
3
4
5
6
7
8
9
class Parent:
name = "Mary"
class Child(Parent):
def func1(self):
print(f"name = {self.name}")
child = Child()
child.func1()
name = Mary
Hi
繼承父類別的方法
Child 子類別透過繼承,擁有父類別的hi()方法,所以可以使用child.hi()。
1
2
3
4
5
6
7
8
9
10
11
class Parent:
name = "Mary"
def hi(self):
print("Hi")
class Child(Parent):
def func1(self):
print(f"name = {self.name}")
child = Child()
child.hi()
Hi
子類別無法讀取父類別私有屬性與方法
1
2
3
4
5
6
7
8
9
10
11
12
class Parent:
__name = "Mary"
def __hi(self):
print("Hi")
class Child(Parent):
def func1(self):
print(f"name = {self.__name}")
self.__hi()
child = Child()
child.func1()
print(f"name = {self.__name}")
^^^^^^^^^^^
AttributeError: 'Child' object has no attribute '_Child__name'
繼承父類別init方法
即便子類別都沒寫任何init方法,也會擁有父類別的init方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Animal:
height = None
weight = None
def __init__(self, height, weight):
self.height = height
self.weight = weight
def get_info(self):
return f"height = {self.height} , weight = {self.weight}"
class Dog(Animal):
pass
dog1 = Dog(100, 5)
print(dog1.get_info())
height = 100 , weight = 5
子類別init方法 有參數
父類別 Animal init方法有二個參數 height(身高) 與 weight(體重)
子類別 Dog 有自己的屬性color。
子類別 Dog 繼承 Animal ,子類別 init 方法,參數一定要有父類別 init 的參數 height, weight(參數位置可以隨意放) ,然後參放再放子類別的 color 屬性。
在子類別 init 方法,使用super().__init__(父類別屬性) 呼叫父類別init方法,初始化父類別的屬性,super()代表父類別。
若父類別與子類有同名get_info()方法,想使用父類別的get_info()方法,使用super().get_info()。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Animal:
height = None
weight = None
def __init__(self, height, weight):
self.height = height
self.weight = weight
def get_info(self):
return f"height: {self.height}, weight: {self.weight}"
class Dog(Animal):
color = None
# 父類別參數位置可以隨便放
def __init__(self, height, weight, color):
super().__init__(height, weight)
self.color = color
def get_info(self):
return f"{super().get_info()} , color: {self.color}"
dog1 = Dog(100, 5, "White")
print(dog1.get_info())
height: 100, weight: 5 , color: White
多繼承
Python 可以多繼承。
語法:
class 子類別(父類別1, 父類別2):
程式碼
若父類別1與父類別2有相同名字的屬性,優先順序由左往右。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Parent1:
name = "Mary"
def hi(self):
print("Parent1.hi()")
class Parent2:
name = "John"
def hi(self):
print("Parent2.hi()")
class Child(Parent1, Parent2):
def func1(self):
print(f"name = {self.name}")
self.hi()
child = Child()
child.func1()
name = Mary
Parent1.hi()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Parent1:
name = "Mary"
def hi(self):
print("Parent1.hi()")
class Parent2:
name = "John"
def hi(self):
print("Parent2.hi()")
class Child(Parent2, Parent1):
def func1(self):
print(f"name = {self.name}")
self.hi()
child = Child()
child.func1()
name = John
Parent2.hi()
呼叫父類別屬性與方法
語法
父類別名.屬性
父類別名.方法(self)
以下是繼承多個父類別,分別呼叫不同父類別的屬性與方法,注意!呼叫父類別.方法(),一定要代入參數self, self 代表子類別記憶體位址。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Parent1:
name = "Mary"
def hi(self):
print("Parent1.hi()")
class Parent2:
name = "John"
def hi(self):
print("Parent2.hi()")
class Child(Parent2, Parent1):
def call_parent1(self):
print(f"name = {Parent1.name}")
Parent1.hi(self)
def call_parent2(self):
print(f"name = {Parent2.name}")
Parent2.hi(self)
child = Child()
child.call_parent1()
child.call_parent2()
name = Mary
Parent1.hi()
name = John
Parent2.hi()
super() 代表「直屬」父類別
child呼叫super().name,取得Father的name。
child呼叫super().hi(),取得Father的hi()方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Grandpa:
name = "Grandpa"
def hi(self):
print("Grandpa.hi()")
class Father(Grandpa):
name = "Father"
def hi(self):
print("Father.hi()")
class Child(Father):
def func1(self):
print(f"name = {super().name}")
super().hi()
child = Child()
child.func1()
name = Father
Father.hi()
若直屬父類別沒有屬性、方法,會找父類別的直屬父類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Grandpa:
name = "Grandpa"
def hi(self):
print("Grandpa.hi()")
class Father(Grandpa):
age = 30
class Child(Father):
def func1(self):
print(f"name = {super().name}")
super().hi()
child = Child()
child.func1()
name = Grandpa
Grandpa.hi()