FireShot Capture 002 - 100 Days of Code_ The Complete Python Pro Bootcamp for 2022 - Udemy_ - www.udemy.com.png

Class Inheritance 類 繼承

FireShot Capture 007 - 100 Days of Code_ The Complete Python Pro Bootcamp for 2022 - Udemy_ - www.udemy.com.png

FireShot Capture 008 - 100 Days of Code_ The Complete Python Pro Bootcamp for 2022 - Udemy_ - www.udemy.com_LI.jpg

程式說明

繼承前

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 ?**