{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "lFft6567_3Yo"
},
"source": [
"# 物件導向 - Object Oriented Programming (OOP)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"本章節內容大綱\n",
"* [Class](#Class)\n",
"* [Class的功能](#Class的功能(Method))\n",
"* [Class的繼承](#Class的繼承(Inheritance))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LAVJmd5E_3Yp"
},
"source": [
"\n",
"## Class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "C0lbw8LP_3Yr"
},
"outputs": [],
"source": [
"name1 = \"Andy\"\n",
"age1 = 25\n",
"height1 = 180\n",
"weight1 = 70\n",
"\n",
"name2 = \"Betty\"\n",
"age2 = 24\n",
"height2 = 157\n",
"weight2 = 49"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "UP0UubnT_3Yt"
},
"outputs": [],
"source": [
"class person:\n",
"\n",
" def __init__(self, name, age, height, weight): # 初始化,每次宣告一個此 Class 的物件時會呼叫\n",
" self.name = name\n",
" self.age = age\n",
" self.height = height\n",
" self.weight = weight\n",
"\n",
" def __repr__(self): # 設定使用 print 印出此類別的物件時要印出的東西 (回傳字串)\n",
" return f'Class Person: {self.name}'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Oh6VDR6n_3Yu"
},
"outputs": [],
"source": [
"p1 = person(\"Andy\", 25, 180, 70)\n",
"p2 = person(\"Betty\", 24, 157, 49)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cNgoF8ui_3Yv"
},
"outputs": [],
"source": [
"print(p1)\n",
"print(p2)\n",
"\n",
"print(p1.name)\n",
"print(p2.age)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kJHuOmRh_3Yx"
},
"source": [
"\n",
"## Class的功能(Method)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Ok_-XbHf_3Yy"
},
"outputs": [],
"source": [
"class person:\n",
"\n",
" def __init__(self, name, age, height, weight, gender):\n",
" self.name = name\n",
" self.age = age\n",
" self.gender = gender\n",
" self.height = height\n",
" self.weight = weight\n",
"\n",
" def __repr__(self):\n",
" return f'Class Person: {self.name}'\n",
"\n",
" # 屬於此類別的 function ,稱作 method\n",
" def BMI(self):\n",
" return round(self.weight / ((self.height/100)**2), 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "IcX3-yqH_3Yz"
},
"outputs": [],
"source": [
"p2 = person(\"Betty\", 24, 157, 49, 'Female')\n",
"\n",
"print(p2.BMI())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ROaVKJP6_3Y0"
},
"source": [
"\n",
"## Class的繼承(Inheritance)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "LvhoyMDw_3Y1"
},
"outputs": [],
"source": [
"class student(person):\n",
"\n",
" def __init__(self, name, age, height, weight, gender, academy, Id, grades):\n",
" super().__init__(name, age, height, weight, gender)\n",
" self.academy = academy\n",
" self.Id = Id\n",
" self.grades = grades\n",
"\n",
" def __repr__(self):\n",
" return f'Student of {self.academy}: {self.name}'\n",
"\n",
" def GPA(self):\n",
" return sum(self.grades)/len(self.grades)\n",
"\n",
"\n",
"class employee(person):\n",
"\n",
" def __init__(self, name, age, height, weight, gender, company, Id, salary, bonus):\n",
" super().__init__(name, age, height, weight, gender)\n",
" self.company = company\n",
" self.Id = Id\n",
" self.salary = salary\n",
" self.bonus = bonus\n",
"\n",
" def __repr__(self):\n",
" return f'Employee of {self.company}: {self.name}'\n",
"\n",
" def annual_salary(self):\n",
" return self.salary * (12 + self.bonus)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FF7AJPqJ_3Y1"
},
"outputs": [],
"source": [
"A = student(\"Betty\", 24, 157, 49, 'Female', 'AIAcademy', '001', [60, 70, 70])\n",
"print(A)\n",
"print(A.GPA())\n",
"print(A.BMI())\n",
"\n",
"B = employee(\"Andy\", 28, 180, 70, 'Male', 'Google', '123654', 100000, 3)\n",
"print(B)\n",
"print(B.annual_salary())\n",
"print(B.BMI())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"---\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Quiz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FmYaj5fP_3Y2"
},
"outputs": [],
"source": [
"# quiz1: 運用 person 內的一些屬性(性別年齡身高體重)實作 BMR 的計算。\n",
"\n",
"# BMR(男)=(13.7×體重(公斤))+(5.0×身高(公分))-(6.8×年齡)+66\n",
"# BMR(女)=(9.6×體重(公斤))+(1.8×身高(公分))-(4.7×年齡)+655\n",
"class person:\n",
"\n",
" def __init__(self, name, age, height, weight, gender):\n",
" self.name = name\n",
" self.age = age\n",
" self.gender = gender # 'Male' or 'Female'\n",
" self.height = height\n",
" self.weight = weight\n",
"\n",
" def __repr__(self):\n",
" return f'Class Person: {self.name}'\n",
"\n",
" def BMR(self):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KgdFihBp_3Y3"
},
"outputs": [],
"source": [
"p1 = person('TA', 20, 170, 55, 'Male')\n",
"print(p1.BMR())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NAtC4THN_3Y3"
},
"outputs": [],
"source": [
"# quiz2: 給定一個正多邊形的父類別,使用繼承分別定出正三角形,正四邊形的子類別。\n",
"\n",
"# 並在正三角形類別實作面積與高的 method。\n",
"# 在正四邊形類別實作面積與對角線長的 method。\n",
"\n",
"class polygon: # 正多邊形\n",
"\n",
" def __init__(self, length):\n",
" self.lenght = length\n",
"\n",
" def __repr__(self):\n",
" return f'Polygon of length {self.length}'\n",
"\n",
"\n",
"class triangle(polygon):\n",
"\n",
" def __init__(self, length):\n",
" pass\n",
"\n",
" def __repr__(self):\n",
" pass\n",
"\n",
" def area(self):\n",
" pass\n",
"\n",
" def height(self):\n",
" pass\n",
"\n",
"\n",
"class square(polygon):\n",
"\n",
" def __init__(self, length):\n",
" pass\n",
"\n",
" def __repr__(self):\n",
" pass\n",
"\n",
" def area(self):\n",
" pass\n",
"\n",
" def diagonal(self):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "q-v0HfFi_3Y5"
},
"outputs": [],
"source": [
"# 可以使用此程式驗證你的 class 有沒有撰寫正確\n",
"\n",
"tri5 = triangle(10)\n",
"print(tri5.area())\n",
"print(tri5.height())\n",
"\n",
"sqr7 = square(7)\n",
"print(sqr7.area())\n",
"print(sqr7.diagonal())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "6 Object Oriented Programming (OOP).ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
},
"vscode": {
"interpreter": {
"hash": "f2c5ff1dcfb7037f8bc8783b5d7cd749bee6b69d77e75d46cb9007d97777ae49"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}