达内 Python

程序: 计算机指令,实现需求的软件

多态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Person():
def __init__(self, name):
self.name = name

def travel(self, vehicle, position):
vehicle.transport(position)

class Vehicle():
def __init__(self):
pass
def transport(self, position):
pass

class Car(Vehicle):
def transport(self, position):
print("car", position)

class Airplane(Vehicle):
def transport(self, position):
print("airplane", position)

person_1 = Person("lucfe")
car_1 = Car()
airplane_1 = Airplane()
person_1.travel(car_1, "beijing")
person_1.travel(airplane_1, "beijing")

car beijing
airplane beijing