當你寫了很多class,然後他每個class有一些相似/重複的功能,你就可以運用物件導向「繼承」的特性
動物
class Dog():
def __init__(self, weight: int, height: int, color: str, age: int):
self.weight = weight
self.height = height
self.color = color
self.age = age
def bark(self):
print("Woof! Woof!")
def info(self):
print(f"This animal is {self.height} cm tall, {self.weight} kg,\n"
f"has a color of {self.color}, and is a {self.age} years olds.")我們先寫個狗的class
member
建構式(Constructor)
method
a = Dog(50, 120, "brown", 10)
a.info()
a.bark()class Cat():
def __init__(self, weight: int, height: int, color: str, age: int):
self.weight = weight
self.height = height
self.color = color
self.age = age
def meow(self):
print("Meow! Meow!")
def info(self):
print(f"This animal is {self.height} cm tall, {self.weight} kg,\n"
f"has a color of {self.color}, and is a {self.age} years olds.")結果你又寫了一個貓的class,然後他們有很多重複的東西
寫兩個class還好,但想像你要寫5個甚至十個很像的class
跟剛剛Dog不一樣的地方
解決方法就是把他們共通的部分寫在一個新的class
class Animal:
def __init__(self, weight: int, height: int, color: str, age: int):
self.height = height
self.weight = weight
self.color = color
self.age = age
def info(self):
print(f"This animal is {self.height} cm tall, {self.weight} kg,\n"
f"has a color of {self.color}, and is a {self.age} years olds.")parent class/ base case/ super class(父類別): 被繼承的類別(共通的部分)
接著加回你原本的Dog 跟 Cat Class,然後讓他們「繼承」 Animal
class Dog(Animal):
def __init__(self, weight: int, height: int, color: str, age: int):
super().__init__(weight, height, color, age)
def bark(self):
print("Woof! Woof!")
class Cat(Animal):
def __init__(self, weight: int, height: int, color: str, age: int):
super().__init__(weight, height, color, age)
def meow(self):
print("Meow! Meow!")在括號裡面填要繼承的class
記得把不一樣的部份加上去
child class/ subclass(子類別):
hm奇怪的東西
可以執行parent class的函式
class Dog(Animal):
def __init__(self, weight: int, height: int, color: str, age: int):
super().__init__(weight, height, color, age)
def bark(self):
print("Woof! Woof!")會執行Animal的建構式
假設你的parent class 有其他函式像是run(),那你就可以在child class裡面用super().run()執行
class Animal:
def __init__(self, weight: int, height: int, color: str, age: int):
self.height = height
self.weight = weight
self.color = color
self.age = age
def __aaa(self):
print("This is a private method")
def _bbb(self):
print("This is a protected method")class Cat(Animal):
def __init__(self, weight: int, height: int, color: str, age: int):
super().__init__(weight, height, color, age)
def meow(self):
super()._bbb()
super().__aaa()我們宣告一個private跟protected的函式
在meow裡面呼叫他
private的會跑不動
class People
class Student
class Teacher
member: name, age
method: greet
member: name, age, student_id
method: greet, study
member: name, age, subject
method: greet, teach
幫我寫這三個class,建議用繼承
class People():
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
class Student(People):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def study(self, subject):
return f"{self.name} is studying {subject}."
class teacher(People):
def __init__(self, name, age, subject):
super().__init__(name, age)
self.subject = subject
def teach(self):
return f"{self.name} is teaching {self.subject}."多形 = 很多型態 = 很多方法
class Animal():
def sound(self):
print("dudududu")
class Dog(Animal):
def sound(self):
print("Woof Woof")
class Cat(Animal):
def sound(self):
print("Meow Meow")
a = Cat()
b = Dog()
c = Animal()
a.sound()
b.sound()
c.sound()function overiding