-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOOP.py
330 lines (255 loc) · 10.8 KB
/
OOP.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# Object Oriented Programming
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 1: Introduction to Python Classes and Instance Methods
# Defining a class named 'Employee'
class Employee:
# Special method called constructor (__init__) to initialize the instance variables
def __init__(self, first, last, pay):
self.first = first # Instance variable for first name
self.last = last # Instance variable for last name
self.pay = pay # Instance variable for pay
self.email = first + '.' + last + '@company.com' # Instance variable for email
# An instance method to return the full name of the employee
def fullname(self):
return '{} {}'.format(self.first, self.last)
# Note: This method is incorrectly named as 'fullname', it should be something like 'apply_raise'.
# It's also incorrectly overwriting the previous 'fullname' method.
# Correcting it to a method for applying a raise:
def apply_raise(self):
self.pay = int(self.pay * 1.04) # Apply a 4% raise to the employee's pay
# Creating two Employee instances with given names and pay
emp_1 = Employee('Bob', 'Bobby', 5000)
emp_2 = Employee('Test', 'User', 6000)
# Printing the email addresses of the employees
print(emp_1.email)
print(emp_2.email)
# Printing the full name of the first employee
# Corrected to use the 'fullname' method
print(emp_1.fullname())
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 2: Inheritance and Method Overriding
# Defining a 'Dog' class
class Dog:
def __init__(self, name, age):
self.name = name # Instance variable for the dog's name
self.age = age # Instance variable for the dog's age
def speak(self):
# Method to print a message including the dog's name and age
print("Hi I am", self.name, "and I am", self.age, "years old")
def talk(self):
# A simple method for the dog to 'talk'
print('Bark')
# Defining a 'Cat' class that inherits from 'Dog'
class Cat(Dog):
def __init__(self, name, age, color):
super().__init__(name, age) # Calling the constructor of the parent 'Dog' class
self.color = color # Additional attribute for the 'Cat' class
def talk(self):
# Overriding the 'talk' method for a cat
print('Meow')
# Creating an instance of the 'Cat' class
tim = Cat('Tim', 5, 'Blue')
tim.speak() # Calling the 'speak' method inherited from 'Dog'
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 3: Basic Class Creation and Instance Usage
# Defining the 'Car' class
class Car:
def __init__(self, make, model, year, color):
self.make = make # Attribute for the make of the car
self.model = model # Attribute for the model of the car
self.year = year # Attribute for the year of the car
self.color = color # Attribute for the color of the car
def drive(self):
# Method to simulate driving the car
print("This car is driving")
def stop(self):
# Method to simulate stopping the car
print("This car is stopped")
# Creating an instance of 'Car'
car1 = Car("Honda", "Accord", 2024, "Silver")
# Accessing attributes of the 'Car' instance
print(car1.make)
print(car1.model)
print(car1.color)
print(car1.year)
# Calling a method of the 'Car' instance
car1.drive()
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 4: Encapsulation and Simple Banking System
# Defining the 'BankAccount' class
class BankAccount:
def __init__(self, initial_balance=0):
self.current_balance = initial_balance # Initializing the balance
def deposit(self, amount):
# Method to deposit money into the account
self.current_balance += amount
def withdraw(self, amount):
# Method to withdraw money from the account
if amount > self.current_balance:
print("Insufficient funds.")
else:
self.current_balance -= amount
def get_balance(self):
# Method to get the current balance
return self.current_balance
# Example usage of the BankAccount class
account = BankAccount(100) # Starting with an initial balance of 100
account.deposit(50) # Depositing 50
account.withdraw(20) # Withdrawing 20
print("Current balance:", account.get_balance()) # Should output 130
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 5: Creating a Car Class with Basic Methods
# Defining the 'Car' class
class Car:
def __init__(self, make, model, year):
self.make = make # Attribute for the make of the car
self.model = model # Attribute for the model of the car
self.year = year # Attribute for the year of the car
def start_engine(self):
# Method to simulate starting the car's engine
print("Engine started.")
def stop_engine(self):
# Method to simulate stopping the car's engine
print("Engine stopped.")
def speak(self):
# Method to display information about the car
print(f"Hello, your car is a {self.make}, model: {self.model}, year: {self.year}")
# Creating an instance of the 'Car' class
car = Car("Toyota", "Camry", 2020)
# Using the methods of the 'Car' instance
car.speak() # Displays information about the car
car.stop_engine() # Simulates stopping the engine
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 6: Encapsulation with Getter and Setter Methods in a Car Class
# Defining the 'Car' class with private attributes
class Car:
def __init__(self, make, model, year):
self._make = make # Private attribute for the make of the car
self._model = model # Private attribute for the model of the car
self._year = year # Private attribute for the year of the car
self._mileage = 0 # Private attribute for the mileage of the car
# Getter method for make
def get_make(self):
return self._make
# Setter method for make
def set_make(self, make):
self._make = make
# Getter method for model
def get_model(self):
return self._model
# Setter method for model
def set_model(self, model):
self._model = model
# Getter method for year
def get_year(self):
return self._year
# Setter method for year
def set_year(self, year):
self._year = year
# Getter method for mileage
def get_mileage(self):
return self._mileage
# Setter method for mileage
def set_mileage(self, mileage):
if mileage >= self._mileage: # Validation to ensure mileage can only increase
self._mileage = mileage
else:
print("Mileage cannot decrease")
# Method to display car information
def display_info(self):
print(f"{self._make} {self._model} ({self._year}), Mileage: {self._mileage} miles")
# Creating an instance of the Car class
my_car = Car("Toyota", "Corolla", 2015)
# Using setter methods to modify the car's attributes
my_car.set_mileage(15000)
# Using the display_info method to show the car's details
my_car.display_info()
# Attempting to set the mileage to a lower value
my_car.set_mileage(14000) # Will not change the mileage and will display a message
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 7: Polymorphism in a Zoo Simulation
# Defining the base class 'Animal'
class Animal:
def __init__(self, name):
self.name = name # Initializing the name of the animal
def make_sound(self):
pass # To be overridden in subclasses
# Defining the 'Lion' class inheriting from 'Animal'
class Lion(Animal):
def make_sound(self):
return "Roar" # Overridden method for Lion
# Defining the 'Elephant' class inheriting from 'Animal'
class Elephant(Animal):
def make_sound(self):
return "Trumpet" # Overridden method for Elephant
# Defining the 'Zoo' class
class Zoo:
def __init__(self):
self.animals = [] # List to store animals
def add_animal(self, animal):
self.animals.append(animal) # Adding an animal to the zoo
def display_sounds(self):
# Displaying the sounds of all animals in the zoo
for animal in self.animals:
print(f"{animal.name} says {animal.make_sound()}")
# Creating a zoo instance
my_zoo = Zoo()
# Creating instances of Lion and Elephant
simba = Lion("Simba")
dumbo = Elephant("Dumbo")
# Adding the animal instances to the zoo
my_zoo.add_animal(simba)
my_zoo.add_animal(dumbo)
# Displaying the sounds of each animal in the zoo
my_zoo.display_sounds()
"""
#----------------------------------------------------------------------------------------------------------------------------------------------
"""
# Lesson 8: Class Methods, Static Methods, and Class Attributes
# Defining the 'Employee' class
class Employee:
_total_employees = 0 # Class attribute to track total number of employees
_next_id = 1 # Class attribute to generate unique IDs
def __init__(self, name, position):
self.name = name # Instance attribute for employee's name
self.position = position # Instance attribute for employee's position
self.id = Employee._generate_id() # Assigning a unique ID to each employee
Employee._total_employees += 1 # Incrementing the total employees count
@classmethod
def _generate_id(cls):
# Class method to generate a unique ID
id = cls._next_id
cls._next_id += 1
return id
@classmethod
def get_total_employees(cls):
# Class method to get the total number of employees
return cls._total_employees
@staticmethod
def company_policy():
# Static method returning the company policy
return "All employees must adhere to the company policy."
# Creating employee instances
emp1 = Employee("Alice", "Developer")
emp2 = Employee("Bob", "Manager")
# Displaying employee IDs
print(f"Employee {emp1.name} ID: {emp1.id}")
print(f"Employee {emp2.name} ID: {emp2.id}")
# Displaying the total number of employees
print("Total employees:", Employee.get_total_employees())
# Accessing the static method to display the company policy
print(Employee.company_policy())
"""