-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathu_classes.py
More file actions
73 lines (58 loc) · 1.72 KB
/
u_classes.py
File metadata and controls
73 lines (58 loc) · 1.72 KB
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
'''
@summary: Python Intro - Classes
'''
# ---------------------------
# class
# ---------------------------
class Vehicle(object):
def __init__(self, name):
""" Constructor """
self.name = name
def __str__(self, *args, **kwargs):
return "Vehicle class" + self.name
# return object.__str__(self, *args, **kwargs)
def __del__(self):
"""
destructor - is called when class is garbage collected
:return:
"""
pass
def move(self, distance=1):
raise NotImplementedError()
def capabilities(self):
"""
:return: list of capabilities
"""
return ["moveable", ]
# ---------------------------
# inheritance
# ---------------------------
class Car(Vehicle):
def __init__(self, make, name):
# Call base class constructor
Vehicle.__init__(self, name)
self.make = make
def move(self, distance=1): # overriding base class method
"""
:param distance: int how far does the car move?
:return:
"""
print "%s-%s moved %d feet" % (self.make, self.name, distance)
def capabilities(self):
parent_capabilities = Vehicle.capabilities(self) # super method call
my_capabilities = ["fuel-using", "transporter"]
return parent_capabilities + my_capabilities
# ---------------------------
# Instantiate no new needed
# ---------------------------
car = Car("Mercedes", "SL500")
print car
# ---------------------------
# access methods with . notation
# ---------------------------
car.move(34)
print car.capabilities()
# ---------------------------
# access properties with . notation
# ---------------------------
print car.name