-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjects_Classes_Inheritance.py
More file actions
99 lines (76 loc) · 4.21 KB
/
Copy pathObjects_Classes_Inheritance.py
File metadata and controls
99 lines (76 loc) · 4.21 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
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
# Object Oriented Programming
# Objects, Attributes and Methods
# Objects are like real world nouns(people,places and things), instances of classes
# Objects can be equal in value but have different refferences(names)
# Attributes are the descriptions for the objects(colour and shape)
# Methods are the actions the object can perform
# Classes
# Class - Defines the attributes and methods of an object, acts as the blueprint for everything in it
# __init__ method is a Python constructor method for initialising an object after it has been created
# self paramater is a convenient way to access and modify an object within a method
class Jeans:
def __init__(self,waist,length,colour):# Constructor method defining the attributes of the jeans class
self.waist = waist # initialising objects with attributes
self.length = length # attribute length with parameter length
self.colour = colour # attribute colour with parameter colour
self.wearing = False # jeans won't always start already being worn so by default its set to false
def put_on(self): # method to put on jeans
print(f"Putting on {self.waist}x{self.length} {self.colour} jeans")
self.wearing = True
def take_off(self): # method to take off jeans
print(f"Taking off {self.waist}x{self.length} {self.colour} jeans")
self.wearing = False
my_jeans = Jeans(31,32,"Blue") # Creates an object of the jeans class and assigns to the variable my_jeans
my_jeans.put_on() # Calls the put_on() method for the my_jeans object
my_jeans.take_off() # Calls the take_off() method for the my_jeans object
print()
my_jeans2 = Jeans(32,34,"Black") # Creates a second object of the jeans class and assigns it to the variable my_jeans2
my_jeans2.put_on() # Calls the put_on() method for the my_jeans2 object
my_jeans2.take_off() # Calls the take_off() method for the my_jeans2 object
print()
my_old_jeans = my_jeans
print(id(my_jeans)) # prints the id for my_jeans object
print(id(my_jeans2)) # slighly different id as it is a different instance of the jeans class
print(id(my_old_jeans)) # same id as my_jeans as it is the same object but with a different name
print()
print("----------------------------------------------------------------------------------------------------------------------")
print()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Inheritance allows a new class (subclass) to inherit attributes and methods from an exisiting, generic class(parent class)
# Inheritance allows code reuse and specilisation
# Overriding is when a subclass/child class provides its own version of a method that replaces the one inherited from the parent/generic class
class Vehicle: # Parent Class
def __init__(self,colour,manufacturer):
self.colour = colour
self.manufacturer = manufacturer
self.fuel = 4 # full tank of fuel
def drive(self):
if self.fuel > 0:
self.fuel - 1
print(f"The {self.colour} {self.manufacturer} goes vroom")
else:
print(f"The {self.colour} {self.manufacturer} is out of fuel")
class Car(Vehicle): # Car inherits the Vehicle class and has its own attributes
def radio(self): # turn on the radio
print("Rockin tunes!")
def window(self): # Opens the window
print("Fresh air!")
class Motorcycle(Vehicle): # Inherits vehicle class but does not get any attributes as a motorcycle wont need a radio or window
def helmet(self): # put helemt on
print("Helmet on")
class ElectricCar(Car):
def drive(self): # overides the drive method of car
print(f"The {self.colour} {self.manufacturer} goes hsssss")
my_car = Car('red','Toyota')
my_car.drive() # can do everything in the vehicle class too
my_car.window()
my_car.radio()
print()
my_bike = Motorcycle('black','Yamaha')
my_bike.drive()
my_bike.helmet() # The only method unique to Motorcycle class
print()
my_electric_car = ElectricCar('white','Tesla')
my_electric_car.drive() # Different drive to car
my_electric_car.radio() # Inherited from Car
my_electric_car.window()# Inherited from Car