1. Învăţa
  2. /
  3. Courses
  4. /
  5. Python 中級オブジェクト指向プログラミング

Connected

exercise

Multiple inheritance

この演習では、Smartphone という新しいクラスを作成して、multiple inheritance(多重継承)を実装する練習をします。

以下に、Smartphone クラスを作る際に使用する Computer クラスと Telephone クラスの定義があります。どちらのクラスもよく確認してください。

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}")

Instrucțiuni 1 / 3

undefined XP
    1
    2
    3
  • Computer と Telephone を継承し、brand、phone_number、music_app を受け取る Smartphone クラスを作成します。
  • Smartphone の親クラスのコンストラクタを呼び出し、インスタンス属性 music_app を定義します。