更新 CLASS/README.md
This commit is contained in:
parent
c4f527faca
commit
812504d3f2
@ -9,7 +9,7 @@
|
||||
|
||||
### 繼承
|
||||
#### 繼承就像是生活中,子女繼承父母的財產一樣。可以直接使用父輩的物品
|
||||
```
|
||||
```python
|
||||
class Lab_location:
|
||||
def __init__(self):
|
||||
self.location = "EL125"
|
||||
@ -34,7 +34,7 @@ student1.lab_detail()
|
||||
|
||||
### 封裝
|
||||
#### 隱藏程式實現細節只保留下接口,使程式容易模組化
|
||||
```
|
||||
```python
|
||||
class Lab_student:
|
||||
def __init__(self,name):
|
||||
self.name = name
|
||||
@ -65,7 +65,7 @@ studen1.crime()
|
||||
### 多形
|
||||
#### 多型指的是一類的事物有多種型態
|
||||
#### 我的理解(1)
|
||||
```
|
||||
```python
|
||||
class A1_student(Lab_location):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@ -98,7 +98,7 @@ student3.lab_detail()
|
||||
2. 呼叫出來的結果都不同
|
||||
|
||||
#### 我的理解(2)
|
||||
```
|
||||
```python
|
||||
class Lab_location:
|
||||
def __init__(self):
|
||||
self.location = "EL125"
|
||||
@ -129,7 +129,7 @@ student3.lab_detail()
|
||||
* Constructor 建構式
|
||||
|
||||
### *命名建議原則:單字字首大寫
|
||||
```
|
||||
```python
|
||||
class Person():
|
||||
def __init__(self,name,age,weight,high):
|
||||
self.eye = 2
|
||||
@ -152,19 +152,19 @@ class Person():
|
||||
```
|
||||
### 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
|
||||
@ -188,7 +188,7 @@ class Person():
|
||||
print(f'{self.name} {self.age}歲 {self.weight}公斤 {high}米 總資產{self.money}')
|
||||
```
|
||||
#### 完整程式碼
|
||||
```
|
||||
```python
|
||||
class Person():
|
||||
def __init__(self,name,age,weight,high):
|
||||
self.eye = 2
|
||||
@ -219,7 +219,7 @@ test_1.person_introduce()
|
||||
* 建立物件時會自動執行__init__
|
||||
* 於建構式中初始化物件的屬性值
|
||||
* 需使用self並用.隔開
|
||||
```
|
||||
```python
|
||||
class Person():
|
||||
def __init__(self,name,age,weight,high):
|
||||
self.eye = 2
|
||||
|
Loading…
Reference in New Issue
Block a user