Python 入門指南 5.0
exercise1609.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | # 引入 Point 定義 from exercise1501 import Point # 引入 Animal 定義 from exercise1606 import Animal # 定義 Mouse class Mouse(Animal): def __init__(self, p, c): super().__init__(p, "鼠", c) self.food = ["鼠", "象"] self.in_river = False # 實作抽象方法 def cross_river(self, d): # 設定進入河中 self.in_river = True # 依據參數 d 進行移動 match d: case "up": self.point.y -= 1 case "down": self.point.y += 1 case "right": self.point.x += 1 case "left": self.point.x -= 1 case _: print("Something wrong!!") # 建立 Mouse 物件 mouse = Mouse(Point(3,3), "Blue") # 印出鼠所在座標 print("動物" + mouse.name + "在" + str(mouse.point)) # 讓鼠進入河中 mouse.cross_river("left") # 印出鼠所在座標 print("動物" + mouse.name + "在" + str(mouse.point)) # 檔名: exercise1609.py # 說明:《Python入門指南》的練習 # 網站: http://kaiching.org # 作者: 張凱慶 # 時間: 2023 年 9 月 |