-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-python-inheritance.py
132 lines (106 loc) · 3.83 KB
/
09-python-inheritance.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# class Vehicle:
# def __init__(self, color, wheels):
# self.color = color
# self.wheels = wheels
# def __str__(self):
# return "color {}, {} wheels".format(self.color, self.wheels)
#
# class Car(Vehicle):
# def __init__(self, color, wheels, speed, displacement):
# self.color = color
# self.wheels = wheels
# self.speed = speed
# self.displacement = displacement
# def __str__(self):
# return "color {}, {} km/h, {} wheels, {} cc".format(self.color, self.speed, self.wheels, self.displacement)
#
#
# c = Car("blue", 150, 4, 1200)
# print(c)
# class Vehicle:
# def __init__(self, color, wheels):
# self.color = color
# self.wheels = wheels
# def __str__(self):
# return "color {}, {} wheels".format(self.color, self.wheels)
#
# class Car(Vehicle):
# def __init__(self, color, wheels, speed, displacement):
# Vehicle.__init__(self, color, wheels)
# self.speed = speed
# self.displacement = displacement
# def __str__(self):
# return Vehicle.__str__(self) + ", {} km/h, {} cc".format(self.speed, self.displacement) # using super()
#
# c = Car("blue", 4, 150, 1200)
# print(c)
#
# class Vehicle:
# def __init__(self, color, wheels):
# self.color = color
# self.wheels = wheels
# def __str__(self):
# return "color {}, {} wheels".format(self.color, self.wheels)
#
# class Car(Vehicle):
# def __init__(self, color, wheels, speed, displacement):
# super().__init__(color, wheels) # using super() without self instead of Vehicle
# self.speed = speed
# self.displacement = displacement
# def __str__(self):
# return super().__str__() + ", {} km/h, {} cc".format(self.speed, self.displacement)
# c = Car("blue", 4, 150, 1200)
# print(c)
class Vehicle:
def __init__(self, color, wheels):
self.color = color
self.wheels = wheels
def __str__(self):
return "color {}, {} wheels".format(self.color, self.wheels)
class Car(Vehicle):
def __init__(self, color, wheels, speed, displacement):
super().__init__(color, wheels) # using super() without self instead of Vehicle
self.speed = speed
self.displacement = displacement
def __str__(self):
return super().__str__() + ", {} km/h, {} cc".format(self.speed, self.displacement)
class Van(Car):
def __init__(self, color, wheels, speed, displacement, load):
super().__init__(color, wheels, speed, displacement)
self.load = load
def __str__(self):
return super().__str__() + ", {} lbs of load".format(self.load)
class Bicycle(Vehicle):
def __init__(self, color, wheels, model):
super().__init__(color, wheels)
self.model = model
def __str__(self):
return super().__str__() + ", {}".format(self.model)
class Motorcycle(Bicycle):
def __init__(self, color, wheels, model, speed, displacement):
super().__init__(color, wheels, model)
self.speed = speed
self.displacement = displacement
def __str__(self):
return super().__str__() + ", {} km/h, {} cc".format(self.speed, self.displacement)
vehicles = [
Car("blue", 4, 150, 1200),
Van("white", 4, 100, 1300, 1500),
Bicycle("green", 2, "btx"),
Motorcycle("black", 2, "ducatti", 180, 900)
]
# def classify(vehicle_list):
# for vehicle in vehicle_list:
# print("{} {}".format(type(vehicle).__name__, vehicle))
# classify(vehicles)
def classify(vehicle_list, wheels=None):
"""
:param wheels:
:type vehicle_list: object
"""
for vehicle in vehicle_list:
if wheels is None:
print("{} {}".format(type(vehicle).__name__, vehicle))
elif vehicle.wheels == wheels:
print("{} {}".format(type(vehicle).__name__, vehicle))
classify(vehicles, 2)