Ex : int, str, dict
HP、x座標、y座標、動作、速度、攻擊頻率、掉落金錢 blah blah
假設你有10隻小兵,那你就要為這隻小兵的每個屬性都設一個變數
7x10 = 70
class 小兵
HP
x座標
y座標
動畫
速度
攻頻
掉落金錢
......
class 小兵
HP
x座標
y座標
動畫
速度
攻頻
掉落金錢
......
小兵也會攻擊,或走路,通常我們會寫一個函式來處理
攻擊
移動
......
每個小兵就可以各自執行他們的函式和計算自己的變數數值
藍圖
小兵(物件)x10
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
一些代表車子性能的變數
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
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(方法)
物件可以執行這個函式
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()所以剛剛用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()加成三個物件
可以看到每個物件的變數和函式是獨立的,不會互相影響
a
b
c
物件
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裡面執行