開始使用免費開始

多重繼承

在這個練習中,你將透過建立一個名為 Smartphone 的新類別來練習實作多重繼承。

下面提供了 ComputerTelephone 類別的定義,之後會用來建立 Smartphone 類別。請仔細查看這兩個類別!

class Computer:
  def __init__(self, brand):
    self.brand = brand

  def browse_internet(self):
    print(f"Using {self.brand}'s default internet browser.")
class Telephone:
  def __init__(self, phone_number):
    self.phone_number = phone_number

  def make_call(self, recipient):
    print(f"Calling {recipient} from {self.phone_number}")

本練習屬於課程

Python 物件導向程式設計進階

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define a Smartphone class that inherits from Computer and
# Telephone, and takes parameters brand, phone_number, and 
# music_app
class ____(____, ____):
  def __init__(self, ____, ____, ____):
    # Call the contructor for the Computer and Telephone
    # class, define the music_app instance-level attribute
    ____.__init__(self, brand)
    Telephone.__init__(self, ____)
    self.music_app = ____
編輯並執行程式碼