Python

class

講師 - 呂家睿

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

學術力

class

類別

class

  • 類別有點像自己寫一個資料型態
  • 寫一個藍圖
  • 有成員(Member)和方法(Methods)

Ex : int, str, dict

教室

類別

Object

物件

類別

資料型態

變數

物件導向 (OOP)

用很多object(class)來寫出程式的一種寫扣方法

  • 把大的專案拆成一個個小部份去寫
  • debug比較快
  • 可以偷別人的來用

Imagine:

小兵

HP、x座標、y座標、動作、速度、攻擊頻率、掉落金錢 blah blah

假設你有10隻小兵,那你就要為這隻小兵的每個屬性都設一個變數

7x10 = 70

class 小兵

HP

x座標

y座標

動畫

速度

攻頻

掉落金錢

......

Imagine:

class 小兵

HP

x座標

y座標

動畫

速度

攻頻

掉落金錢

......

小兵也會攻擊,或走路,通常我們會寫一個函式來處理

攻擊

移動

......

每個小兵就可以各自執行他們的函式和計算自己的變數數值

藍圖

小兵(物件)x10

coding time

coding time

class Car():
    price:        int = 100
    color:        str = "red"
    horse_power:  int = 100
    brand :       str = "toyota"
a = Car()
print(a.price)
print(a.color)
print(a.horse_power)
print(a.brand)

輸出結果:

宣告一個物件

把這個物件的變數印出來

宣告一個class(類別)叫做Car

一些代表車子性能的變數

coding time

a = Car()
print(a.price)
a.price = 200
print(a.price)

記憶體位置

class

輸出結果:

輸出結果:

可以改物件成員的值

a = Car()
print(a)

你們可以試試看把物件拿去print

建構式

class Car():

    wheels = 4;

    def __init__(self, price: int, color: str, 
                 horse_power: int, brand: str):
        self.price        = price
        self.color        = color
        self.horse_power  = horse_power
        self.brand        = brand

有沒有辦法創建物件的時候直接把成員的數值搞定呢?

a = Car(1000, "blue", 200, "honda")
print(a.price)
print(a.color)
print(a.horse_power)
print(a.brand)

constructor(建構式)

物件被創造的時候會跑這個函式

一個特殊的關鍵字,代表這個物件自己

對他一定要叫這個名字

class property

instance property

Method(方法)

class Car():

    wheels = 4;

    def __init__(self, price: int, color: str, 
                 horse_power: int, brand: str):
        self.price        = price
        self.color        = color
        self.horse_power  = horse_power
        self.brand        = brand
    
    def info(self):
        print(f"price: {self.price}")
        print(f"color: {self.color}")
        print(f"horse_power: {self.horse_power}")
        print(f"brand: {self.brand}")

class裡面的函式

a = Car(1000, "blue", 200, "honda")
a.info()

輸出結果:

method(方法)

物件可以執行這個函式

練習題

  • 寫一個類別(class)叫做People
  • 要有處存身高、體重、名字和喜歡的食物的成員(member) 
  • 有一個方法(method)可以自我介紹
me = People(178, 49, "ray", "pizza")
me.intro()

輸出結果:

物件的建構和使用:

答案

class People():
    def __init__(self, height: int, weight: int, name: str, fav_food: str):
        self.height = height
        self.weight = weight
        self.name = name
        self.fav_food = fav_food

    def intro(self):
        print("My height is", self.height, "cm")
        print("My weight is", self.weight, "kg")
        print("People like to call me", self.name)
        print("My favorite food is", self.fav_food)

me = People(178, 49, "ray", "pizza")
me.intro()

encapsulation

封裝

物件導向四大特性

Encapsulation(封裝)

Inheritance(繼承)

Polymorphism(多形)

Abstraction(抽象化)

封裝(Encapsulation)

所以剛剛用class寫了半天,感覺他也沒有很厲害。現在我們多加幾個物件試試看

class Car():

    wheels = 4;

    def __init__(self, price: int, color: str, 
                 horse_power: int, brand: str):
        self.price        = price
        self.color        = color
        self.horse_power  = horse_power
        self.brand        = brand
    
    def info(self):
        print(f"price: {self.price}")
        print(f"color: {self.color}")
        print(f"horse_power: {self.horse_power}")
        print(f"brand: {self.brand}")

剛剛我們寫的扣

a = Car(1000, "blue", 200, "honda")
b = Car(2000, "white", 300, "BMW")
c = Car(5000, "red", 400, "Ferari")
a.info()
b.info()
c.info()

加成三個物件

封裝(Encapsulation)

可以看到每個物件的變數和函式是獨立的,不會互相影響

a

b

c

物件

private

public

protected

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

飯粒

class Car():
    def __init__(self, price: int, color: str, 
                 horse_power: int, brand: str):
        self.price        = price
        self.__color        = color
        self.__horse_power  = horse_power
        self.brand        = brand
    
    def info(self):
        print(f"price: {self.price}")
        print(f"color: {self.__color}")
        print(f"horse_power: {self.__horse_power}")
        print(f"brand: {self.brand}")
a = Car(1000, "blue", 200, "honda")
a.info()
print(a.price)
print(a.brand)
print(a.__color)
print(a.__horse_power)

把color跟horse_power改成private

在class裡面執行

在class外面執行

飯粒

class Car():
    def __init__(self, price: int, color: str, 
                 horse_power: int, brand: str):
        self.price        = price
        self.__color        = color
        self.__horse_power  = horse_power
        self.brand        = brand
    
    def info(self):
        print(f"price: {self.price}")
        print(f"color: {self.__color}")
        print(f"horse_power: {self.__horse_power}")
        print(f"brand: {self.brand}")
        print(f"This car is {self.__rate()}")
    
    def __rate(self):
        if (self.__horse_power > 250):
            return "fantastic"
        else:
            return "ok"
a = Car(1000, "blue", 200, "honda")
a.info()
a.__rate()

private method

算這個人的bmi

在class外面執行

在class裡面執行

繼承(Inheritance)

多型(Polymorphism)

下一堂課

下課!!!!!

python-6

By ck11300111呂家睿

python-6

  • 167