Python

class - Part 2

講師 - 呂家睿

  • 建中資訊38屆學術長+副社
  • 玩原神跟雀魂
  • 頭像是應急食品
  • 被電爛
  • 有問題歡迎來問我啊
  • C++
  • python
  • 競程是什麼可以吃嗎?
  • 數學好難
  • 雖然很菜但喜歡遊戲開發
  • 打開電腦

學術力

物件導向四大特性

Encapsulation(封裝)

Inheritance(繼承)

Polymorphism(多形)

Abstraction(抽象化)

Inheritance

繼承

繼承(Inheritance)

當你寫了很多class,然後他每個class有一些相似/重複的功能,你就可以運用物件導向「繼承」的特性

動物

繼承(Inheritance)

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()

繼承(Inheritance)

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不一樣的地方

繼承(Inheritance)

解決方法就是把他們共通的部分寫在一個新的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(父類別):  被繼承的類別(共通的部分)

繼承(Inheritance)

接著加回你原本的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奇怪的東西

super()

可以執行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()執行

如果你還是不懂什麼是繼承的話,那妳可以看這個影片

private

public

protected

  • 預設是public
  • 在class裡面、外面都可以用
  • 前面加上__(兩個底線)
  • 只有在這個class裡面可以用
  • child class不能用
  • 前面加上_(一個底線)
  • 原則上不會在class, child class外使用
  • 不是強制

複習一下

testing

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}."

Polymorphism

多形

多形(Polymorphism)

多形 = 很多型態 = 很多方法

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

下課!!!!!

python-7

By ck11300111呂家睿

python-7

  • 156