开始使用免费开始使用

多重继承

在本练习中,您将通过创建一个名为 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 = ____
编辑并运行代码