Python Python Programming Class and Object l Object
Python程式設計 Python Programming Class and Object
大綱 l 物件導向程式設計(Object Oriented Programming) l 物件(Object)及類別(Class) l 類別組合(Composition) l 類別繼承(Inheritance) l 類別多型(Polymorphism) 1
類別範例 class First. Class: """My first class in python. """ str = "Apple" def fun(self): # 在類別內的函數都至少要傳入參數self return "Hello world. " my_obj = First. Class() # 宣告一個類別為First. Class的物件 print(my_obj. str) # 使用物件的公開變數 print(my_obj. fun()) # 使用物件的公開函數 8
學生類別範例 class Student: def __init__(self, no, name): # 學生的建構子 self. no = no # 設定學號 self. name = name # 設定姓名 def set. Student(self, no, name): # 學生的變異子 self. no = no # 設定學號 self. name = name # 設定姓名 def get. No(self): # 學生的存取子 return no # 傳回學號 def get. Name(self): # 學生的存取子 return name # 傳回姓名 def __str__(self): # 傳回學生字串 return "%s %s" % (self. no, self. name) no = input("Enter student number: ") name = input("Enter student name: ") student = Student(no, name) print(student) 11
學生類別範例 l 執行的結果 Enter student number: 10740105 Enter student name: Taylor Swift 10740105 Taylor Swift 12
帳戶類別 class Account: def __init__(self, number = '', name = ''): self. number = number self. name = name self. balance = 0 def deposit(self, amount): #存款動作: amount代表存入金額 if amount <= 0: raise Value. Error('must be positive') self. balance += amount def withdraw(self, amount): #取款動作: amount代表取款金額 if amount <= self. balance: self. balance -= amount else: raise Runtime. Error('balance not enough') 13
帳戶類別 acct = Account() acct. number = '123 -456 -789' acct. name = 'Justin' acct. balance = 0 print(acct. number) # 123 -456 -789 print(acct. name) # Justin deposit(acct, 100) print(acct. balance) # 100 withdraw(acct, 50) print(acct. balance) # 50 14
帳戶類別範例 l 執行的結果 123 -456 -789 Justin 100 50 15
點類別範例 from math import * class Point: def __init__(self, x = 0, y = 0): # 點的建構子 (x, y) self. x = x self. y = y def set. Point(self, x, y): # 點的變異子 (x, y) self. x = x self. y = y def get. X(self): # 點x座標的存取子 return self. x def get. Y(self): # 點y座標的存取子 return self. y 16
點類別範例 def __str__(self): # 點座標字串 (x, y) return "(%f, %f)" % (self. x, self. y) def distance(a, b): # 2點座標間的距離 return sqrt(pow(a. x - b. x, 2) + pow(a. y - b. y, 2)) x 1, y 1, x 2, y 2 = input("Enter 2 points: "). split(' ') x 1, y 1, x 2, y 2 = float(x 1), float(y 1), float(x 2), float(y 2) a, b = Point(x 1, y 1), Point(x 2, y 2) print("The distance between", a, "and", b, "is", distance(a, b)) 17
點類別範例 l 執行的結果 Enter 2 points: 0 3 4 0 The distance between (0. 000000, 3. 000000) and (4. 000000, 0. 000000) is 5. 0 18
類別組合 (Composition) – 三角形範例 from math import * class Point: def __init__(self, x = 0, y = 0): # 點的建構子 (x, y) self. x = x self. y = y def set. Point(self, x, y): # 點的變異子 (x, y) self. x = x self. y = y def get. X(self): # 點x座標的存取子 return self. x def get. Y(self): # 點y座標的存取子 return self. y def __str__(self): # 點座標字串 (x, y) return "(%f, %f)" % (self. x, self. y) def distance(a, b): # 2點座標間的距離 return sqrt(pow(a. x - b. x, 2) + pow(a. y - b. y, 2)) 19
類別組合 (Composition) – 三角形範例 from math import * class Point: def __init__(self, x = 0, y = 0): # 點的建構子 (x, y) self. x = x self. y = y def set. Point(self, x, y): # 點的變異子 (x, y) self. x = x self. y = y def get. X(self): # 點x座標的存取子 return self. x def get. Y(self): # 點y座標的存取子 return self. y def __str__(self): # 點座標字串 (x, y) return "(%f, %f)" % (self. x, self. y) def distance(a, b): # 2點座標間的距離 return sqrt(pow(a. x - b. x, 2) + pow(a. y - b. y, 2)) 20
類別組合 (Composition) – 三角形範例 class Triangle: def __init__(self, a, b, c): # 三角形的建構子 (a, b, c) self. a = a self. b = b self. c = c def set. Point(self, a, b, c): # 三角形的變異子 (a, b, c) self. a = a self. b = b self. c = c def get. A(self): # 三角形點a座標的存取子 return self. a def get. B(self): # 三角形點b座標的存取子 return self. b def get. C(self): # 三角形點c座標的存取子 return self. c def __str__(self): # 三角形座標字串 return str(a) + ", " + str(b) + ", " + str(c) 21
類別組合 (Composition) – 三角形範例 def area(t): a, b, c = distance(t. a, t. b), distance(t. b, t. c), distance(t. c, t. a) if (a + b > c and b + c > a and c + a > b): s = (a + b + c)/2 return sqrt(s * (s - a) * (s - b) * (s - c)) else: return 0 x 1, y 1, x 2, y 2, x 3, y 3 = input("Enter 3 points: "). split(' ') x 1, y 1, x 2, y 2, x 3, y 3 = float(x 1), float(y 1), float(x 2), float(y 2), float(x 3), float(y 3) a, b, c = Point(x 1, y 1), Point(x 2, y 2), Point(x 3, y 3) t = Triangle(a, b, c) print("The area of the triangle", t, "is", area(t)) 22
類別組合 (Composition) – 三角形範例 l 執行的結果 Enter 3 points: 0 0 0 3 4 0 The area of the triangle (0. 000000, 0. 000000), (0. 000000, 3. 000000), (4. 000000, 0. 000000) is 6. 0 23
類別繼承(Inheritance) – 汽車範例 class Car: wheels_number = 4 # 輪胎數量 car_doors = 4 # 車門數量 passengers = 4 # 乘客數量 def __init__(self, wheels_number=4, car_doors=4, passengers=4): self. wheels_number = wheels_number self. car_doors = car_doors self. passengers = passengers 24
類別繼承(Inheritance) – 汽車範例 # SUV也是一種車子,所以繼承Car class SUV(Car): brand_name = "" # 品牌名稱 air_bag = 2 # 安全氣囊數 sunroof = True # 是否擁有天窗 def __init__(self, wheels_number, car_doors, passengers, brand_name="", air_bag=2, sunroof=False): super(). __init__(wheels_number, car_doors, passengers) self. brand_name = brand_name self. air_bag = air_bag self. sunroof = sunroof 25
類別繼承(Inheritance) – 汽車範例 def get. Details(self): print("==== Details ====") print("Brand: ", self. brand_name) print("Wheels number: ", self. wheels_number) # 可直接呼叫父類別的變數( 屬性) print("Doors number: ", self. car_doors) # 可直接呼叫父類別的變數(屬性 ) print("Air-bags number: ", self. air_bag) print("Sunroof: ", self. sunroof) print("=========") 26
類別繼承(Inheritance) – 汽車範例 # 宣告一台Toyota RAV的休旅車(SUV) toyota_rav = SUV(4, 5, 5, "Toyota RAV", 4, True) toyota_rav. get. Details() # 宣告一台BMW X 5的休旅車 bmw_x 5 = SUV(4, 5, 5, "BMW X 5", 6, True) bmw_x 5. get. Details() 27
類別繼承(Inheritance) – 汽車範例 l 執行的結果 ==== Details ==== Brand: Toyota RAV Wheels number: 4 Doors number: 5 Air-bags number: 4 Sunroof: True ========= Details ==== Brand: BMW X 5 Wheels number: 4 Doors number: 5 Air-bags number: 6 Sunroof: True ========= 28
類別多型 (Polymorphism) – 汽車範例 class Car: # wheels_number: 輪胎數量, car_doors: 車門數量, passengers: 乘客數量 def __init__(self, wheels_number=4, car_doors=4, passengers=4): self. wheels_number = wheels_number self. car_doors = car_doors self. passengers = passengers def drive(self): print("Drive a car. ") 29
類別多型 (Polymorphism) – 汽車範例 # SUV也是一種車子,所以繼承Car class SUV(Car): # brand_name: 品牌名稱, air_bag: 安全氣囊數, sunroof: 是否擁有天窗 def __init__(self, wheels_number, car_doors, passengers, brand_name="", air_bag=2, sunroof=False): super(). __init__(wheels_number, car_doors, passengers) self. brand_name = brand_name self. air_bag = air_bag self. sunroof = sunroof # 覆寫父類別的drive 30
類別多型 (Polymorphism) – 汽車範例 # 覆寫父類別的drive def drive(self): print("Drive this {0} to my vacation. ". format(self. brand_name)) def get. Details(self): print("==== Details ====") print("Brand: ", self. brand_name) print("Wheels number: ", self. wheels_number) # 可直接呼叫父類別的變數(屬性) print("Doors number: ", self. car_doors) # 可直接呼叫父類別的變數(屬性) print("Air-bags number: ", self. air_bag) print("Sunroof: ", self. sunroof) print("=========") 31
類別多型 (Polymorphism) – 汽車範例 # Bus也是一種車子,所以繼承Car class Bus(Car): # brand_name: 品牌名稱, air_bag: 安全氣囊數, sunroof: 是否擁有天窗 def __init__(self, wheels_number, car_doors, passengers, brand_name="", air_bag=0): super(). __init__(wheels_number, car_doors, passengers) self. brand_name = brand_name self. air_bag = air_bag 32
類別多型 (Polymorphism) – 汽車範例 # 覆寫父類別的drive def drive(self): print("Take this {0} to my vacation. ". format(self. brand_name)) def get. Details(self): print("==== Details ====") print("Brand: ", self. brand_name) print("Wheels number: ", self. wheels_number) # 可直接呼叫父類別的變數(屬性) print("Doors number: ", self. car_doors) # 可直接呼叫父類別的變數(屬性) print("Air-bags number: ", self. air_bag) print("=========") 33
類別多型 (Polymorphism) – 汽車範例 # 宣告一台Toyota RAV的休旅車(SUV) toyota_rav = SUV(4, 5, 5, "Toyota RAV", 4, True) # 宣告一台BMW X 5的休旅車 bmw_x 5 = SUV(4, 5, 5, "BMW X 5", 6, True) # 宣告一台Volvo Bus的巴士 volvo_bus = Bus(4, 3, 50, "Volvo Bus", 0) # 分別呼叫各種車輛的drive()方法 def lets. Drive(cars): for car in cars: car. drive() lets. Drive([toyota_rav, bmw_x 5, volvo_bus]) 34
類別多型 (Polymorphism) – 汽車範例 l 執行的結果 Drive this Toyota RAV to my vacation. Drive this BMW X 5 to my vacation. Take this Volvo Bus to my vacation. 35
- Slides: 36