MulaiMulai sekarang secara gratis

Multiple inheritance

Dalam latihan ini, Anda akan berlatih menerapkan multiple inheritance dengan membuat kelas baru bernama Smartphone.

Di bawah ini adalah definisi untuk kelas Computer dan Telephone, yang akan digunakan saat membuat kelas Smartphone. Pastikan Anda mencermati kedua kelas ini!

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

Latihan ini adalah bagian dari kursus

Pemrograman Berorientasi Objek Tingkat Menengah di Python

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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 = ____
Edit dan Jalankan Kode