
Class Inheritance 類 繼承


程式說明
繼承前
class Animal:
def __init__(self):
self.num_eyes = 2 #眼睛
def breathe(self):
print("Inhale, exhale.") #呼吸
class Fish:
def swim(self):
print("moving in water.")
nemo = Fish()
nemo.swim(()
繼承後
class Animal:
def __init__(self):
self.num_eyes = 2 #眼睛
def breathe(self):
print("Inhale, exhale.") #呼吸
class Fish(Animal):
def __init__(self):
super().__init__()
def swim(self):
print("moving in water.")
nemo = Fish()
nemo.swim(()
nemo.breathe()
print(nemo.num_eyes)
繼承後修改
class Animal:
def __init__(self):
self.num_eyes = 2 #眼睛
def breathe(self):
print("Inhale, exhale.") #呼吸
class Fish(Animal):
def __init__(self):
super().__init__()
def breathe(self):
super().breathe()
print("doing this underwater.")
def swim(self):
print("moving in water.")
nemo = Fish()
nemo.swim(()
nemo.breathe()
print(nemo.num_eyes)
Detect Collisions with Food
##food.py
from turtle import Turtle
class Food:
def __init__(self):
**#承上開始對類的了解,我們該如何使 class Food 成為turtle 的 subclass ?**