W3School TIY Editor

  • W3School 在线教程
  • 改变方向
  • 暗黑模式
​x
 
class Vehicle:
  def __init__(self, brand, model):
    self.brand = brand
    self.model = model
​
  def move(self):
    print("移动!")
​
class Car(Vehicle):
  pass
​
class Boat(Vehicle):
  def move(self):
    print("航行!")
​
class Plane(Vehicle):
  def move(self):
    print("飞行!")
​
car1 = Car("福特", "野马") #创建 Car 对象
boat1 = Boat("伊比萨", "Touring 20") #创建 Boat 对象
plane1 = Plane("波音", "747") #创建 Plane 对象
​
for x in (car1, boat1, plane1):
  print(x.brand)
  print(x.model)
  x.move()