Quick actions

cmd+k|ctrl+k

Navigation

Languages

class

Snippet info

Language

Python

Visibility

public

Author

ttmo04450

Created

2026-07-10T06:24:37.489465Z

Updated

2026-07-10T07:20:39.430903Z

class Toy:
    head = "circle"  # 頭的形狀預設為 "circle"
    hands = 2        # 手的數量預設為 2
    legs = 2         # 腳的數量預設為 2

    def __init__(self, x, y, name):
        self.name = name  # 將傳入的 name 參數存入實例屬性 self.name
        self.body = x     # 將傳入的 x 參數存入實例屬性 self.body(在 babie_baby 中代表 'short')
        self.hat = y      # 將傳入的 y 參數存入實例屬性 self.hat(在 babie_baby 中代表 'white')

    def run(self,):
        print(f"{self.name} is running")


class Babie(Toy):                               # Babie 子類別(Child Class)繼承自Toy
    def __init__(self, x, y, name, hair):       # 繼承自Toy參數  + hair' 參數
        super().__init__(x, y, name)            # 使用 super() 呼叫父Toy參數
        self.hair = hair                        # 將傳入的 hair 參數存入實例屬性 self.hair
        print(f"Babie: hair {self.hair}")       # 當物件被建立時,立即印出該 Babie 的髮色

    def jump(self):
        print(f"Babie: {self.name} jump")

                            # 覆寫(Overwrite)父類別的 run 方法,並加入額外功能
    def run(self):
        super().run()       # 先呼叫父類別Toy的 run() 方法,印出「[名字] is running」
        print(f"{self.name} don't like to run")     # 接著印出子類別額外想顯示的訊息「[名字] don't like to run」


babie_baby = Babie('short', 'white', 'bb', 'black')

babie = Babie('tall', 'green', 'bb-girl', 'red')

print(babie_baby.name)

print(babie_baby.body)

babie_baby.run()

babie_baby.jump()

print(babie.head)

Toy.a()
INFO