|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Q1. 設計一個 MySchool\n", |
| 8 | + "\n", |
| 9 | + "這個類別屬性要包含學生姓名、科目、成績,並且設計一個方法可以輸入姓名和科目查詢學生的成績\n", |
| 10 | + "\n", |
| 11 | + "設計方向為以下:\n", |
| 12 | + "\n", |
| 13 | + "- 輸入三個參數 myschool = MySchool('Erik', 'Math', '80')\n", |
| 14 | + "- 這個方法要能回傳 'Hello ! Erik, the score of Math is 80.'" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 12, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "class MySchool:\n", |
| 24 | + " \n", |
| 25 | + " def __init__(self, name, subject, score):\n", |
| 26 | + " self.name = name\n", |
| 27 | + " self.subject= subject\n", |
| 28 | + " self.score = score\n", |
| 29 | + " \n", |
| 30 | + " def get_score(self):\n", |
| 31 | + " return f'Hello ! {self.name}, the score of {self.subject} is {self.score}.'" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": 6, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [ |
| 39 | + { |
| 40 | + "data": { |
| 41 | + "text/plain": [ |
| 42 | + "'Hello ! Erik, the score of Math is 80.'" |
| 43 | + ] |
| 44 | + }, |
| 45 | + "execution_count": 6, |
| 46 | + "metadata": {}, |
| 47 | + "output_type": "execute_result" |
| 48 | + } |
| 49 | + ], |
| 50 | + "source": [ |
| 51 | + "myschool = MySchool('Erik', 'Math', '80')\n", |
| 52 | + "myschool.get_score()" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "## Q2. 抽象化\n", |
| 60 | + "\n", |
| 61 | + "請將這兩個物件抽象化,相同方法放在基底,其他利用繼承\n", |
| 62 | + "\n", |
| 63 | + "``` python=\n", |
| 64 | + "class Car:\n", |
| 65 | + "\n", |
| 66 | + " def drive(self):\n", |
| 67 | + " print(\"drive method is called.\")\n", |
| 68 | + "\n", |
| 69 | + " def accelerate(self):\n", |
| 70 | + " print(\"accelerate method is called.\")\n", |
| 71 | + " \n", |
| 72 | + "\n", |
| 73 | + "class Airplane:\n", |
| 74 | + "\n", |
| 75 | + " def drive(self):\n", |
| 76 | + " print(\"drive method is called.\")\n", |
| 77 | + "\n", |
| 78 | + " def fly(self):\n", |
| 79 | + " print(\"fly method is called.\")\n", |
| 80 | + "```\n", |
| 81 | + "\n", |
| 82 | + "設計方向為以下:\n", |
| 83 | + "\n", |
| 84 | + "- 基底設計一個 Transportation,並給予顏色屬性為黑色\n", |
| 85 | + "- 將汽車和飛機變成子類別" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 15, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "class Transportation:\n", |
| 95 | + "\n", |
| 96 | + " def __init__(self):\n", |
| 97 | + " self.color = \"black\"\n", |
| 98 | + " \n", |
| 99 | + " def drive(self):\n", |
| 100 | + " print(\"drive method is called.\")\n", |
| 101 | + "\n", |
| 102 | + "class Car(Transportation):\n", |
| 103 | + "\n", |
| 104 | + " def accelerate(self):\n", |
| 105 | + " print(\"accelerate is method called.\")\n", |
| 106 | + "\n", |
| 107 | + "class Airplane(Transportation):\n", |
| 108 | + "\n", |
| 109 | + " def fly(self):\n", |
| 110 | + " print(\"fly method is called.\")" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": null, |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [], |
| 118 | + "source": [] |
| 119 | + } |
| 120 | + ], |
| 121 | + "metadata": { |
| 122 | + "kernelspec": { |
| 123 | + "display_name": "Python 3", |
| 124 | + "language": "python", |
| 125 | + "name": "python3" |
| 126 | + }, |
| 127 | + "language_info": { |
| 128 | + "codemirror_mode": { |
| 129 | + "name": "ipython", |
| 130 | + "version": 3 |
| 131 | + }, |
| 132 | + "file_extension": ".py", |
| 133 | + "mimetype": "text/x-python", |
| 134 | + "name": "python", |
| 135 | + "nbconvert_exporter": "python", |
| 136 | + "pygments_lexer": "ipython3", |
| 137 | + "version": "3.8.3" |
| 138 | + } |
| 139 | + }, |
| 140 | + "nbformat": 4, |
| 141 | + "nbformat_minor": 4 |
| 142 | +} |
0 commit comments