# 物件導向-Class ## 物件導向 ### 以物件的方式去描述資料應有的行為 * 像我們在現實世界中定義貓、狗、人、車等等一樣,都有自己的運作方式 ### 主要特性: 1. 繼承 2. 封裝 3. 多型 ### 繼承 #### 繼承就像是生活中,子女繼承父母的財產一樣。可以直接使用父輩的物品 ```python class Lab_location: def __init__(self): self.location = "EL125" class Lab_student(Lab_location): def __init__(self,student): super().__init__() self.student = student def lab_detail(self): print(f'student : {self.student}') # 原本定義的 print(f'Lab_位置 : {self.location}') # 可以從Lab_location(父輩) 抓出物件 student1 = Lab_student("M11112030") student1.lab_detail() ``` 1. Class - Lab_location 是位置 2. Class - Lab_student 繼承Lab_location的物件 3. super().__init__() 繼承Lab_location ### 封裝 #### 隱藏程式實現細節只保留下接口,使程式容易模組化 ```python class Lab_student: def __init__(self,name): self.name = name self.profession = "警察" def student_name(self): print(f'name : {self.name}') def crime(self): print(f'{self.name} 要來逮捕你了') def __no_crime(self): print(f'{self.name} 從你身邊路過') studen1 = Lab_student("王希名") studen1.student_name() studen1.crime() ``` 1. 先將student1 變成Lab_student的物件形式 2. 呼叫student_name的時候會觸發 3. 呼叫crime的時候會觸發 4. 呼叫__no_crime不會觸發 *(加入2個底線,就變成私有變數,外界呼叫不到) ### 多形 #### 多型指的是一類的事物有多種型態 #### 我的理解(1) ```python class A1_student(Lab_location): def __init__(self): super().__init__() def lab_detail(self): print(f'M11112030 在 {self.location}') class A2_student(Lab_location): def __init__(self): super().__init__() def lab_detail(self): print(f'M11112031 在 {self.location}') class A3_student(Lab_location): def __init__(self): super().__init__() def lab_detail(self): print(f'M11112032 在 {self.location}') student1 = A1_student() student2 = A2_student() student3 = A3_student() student1.lab_detail() student2.lab_detail() student3.lab_detail() ``` 1. A1、A2、A3都有leb_detail的方式 2. 呼叫出來的結果都不同 #### 我的理解(2) ```python class Lab_location: def __init__(self): self.location = "EL125" class Lab_student(Lab_location): def __init__(self,name): super().__init__() self.name = name def lab_detail(self): print(f'{self.name} 在 {self.location}') student1 = Lab_student("A") student2 = Lab_student("B") student3 = Lab_student("C") student1.lab_detail() student2.lab_detail() student3.lab_detail() ``` 1. 同樣呼叫Lab_student 但可以不同型態 ## ## Class ### Class類別 * Object 物件 * Attribute 屬性 * Method 方法 * Constructor 建構式 ### *命名建議原則:單字字首大寫 ```python class Person(): def __init__(self,name,age,weight,high): self.eye = 2 self.hand = 2 self.noise = 1 self.leg = 2 self.money =0 self.name = name self.age = age self.weight = weight self.high = high def person_body_detail(self): print(f'眼睛數量 : {self.eye}') print(f'手數量 : {self.hand}') print(f'鼻子數量 : {self.noise}') print(f'腿數量 : {self.leg}') def person_introduce(self): high = float(self.high/100) print(f'{self.name} {self.age}歲 {self.weight}公斤 {high}米 總資產{self.money}') ``` ### Object 物件 * 透過Class建立實體 ```python test_1 = Person(name='王希銘',age = 37,weight=105,high=168) ``` * ### Attribute 屬性 * 放置物件的資料 * 建立物件後,才可進行屬性值的設定 * 建議使用建構式來進行屬性值的設定 ```python test_1.money = 20000 ``` ### Method 方法 * 與函式(function)很類似 都是def 開頭 但必須攜帶self ```python class Person(): def __init__(self,name,age,weight,high): self.eye = 2 self.hand = 2 self.noise = 1 self.leg = 2 self.money =0 self.name = name self.age = age self.weight = weight self.high = high # Method def person_body_detail(self): print(f'眼睛數量 : {self.eye}') print(f'手數量 : {self.hand}') print(f'鼻子數量 : {self.noise}') print(f'腿數量 : {self.leg}') # Method def person_introduce(self): high = float(self.high/100) print(f'{self.name} {self.age}歲 {self.weight}公斤 {high}米 總資產{self.money}') ``` #### 完整程式碼 ```python class Person(): def __init__(self,name,age,weight,high): self.eye = 2 self.hand = 2 self.noise = 1 self.leg = 2 self.money =0 self.name = name self.age = age self.weight = weight self.high = high def person_body_detail(self): print(f'眼睛數量 : {self.eye}') print(f'手數量 : {self.hand}') print(f'鼻子數量 : {self.noise}') print(f'腿數量 : {self.leg}') def person_introduce(self): high = float(self.high/100) print(f'{self.name} {self.age}歲 {self.weight}公斤 {high}米 總資產{self.money}') test_1 = Person(name='王希銘',age = 37,weight=105,high=168) test_1.money = 20000 test_1.person_body_detail() test_1.person_introduce() ``` ### Constructor 建構式 - self * 建立物件時會自動執行__init__ * 於建構式中初始化物件的屬性值 * 需使用self並用.隔開 ```python class Person(): def __init__(self,name,age,weight,high): self.eye = 2 self.hand = 2 self.noise = 1 self.leg = 2 self.money =0 self.name = name self.age = age self.weight = weight self.high = high ``` * self代表這個物件 * 不太正確但好理解說法:這個Class的全域變數 ### Method 方法和 function 函式差異 * Function是定義在class外面的 Method定義在class內部的 * Function是所有地方可以使用 Method限定使用 * Function只能使用帶進來的變數或是全域變數 * Method可以使用帶進來的變數和class的所有變數