-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_ten.py
371 lines (349 loc) · 9.57 KB
/
10_ten.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# object-oriented programming
# 1.Basic class & object
# Create a car class with attributes like brand & model.Then create an instance
# of this class.
# note:here self is similar to 'this' in java
# class Car:
# # init is also called as 'constructor'
# def __init__(self, brand, model):
# self.brand=brand
# self.model=model
# # object creation
# my_car = Car("Toyota", "Corolla")
# print(my_car.brand)
# print(my_car.model)
#
# my_new_car = Car("Tata", "Safari")
# print(my_car.brand)
# print(my_car.model)
# 2.Class method and Self
# Add a method to the Car class that displays the full name of the car(brand &
# model)
# class Car:
# def __init__(self, company,model):
# self.company = company
# self.model = model
#
# def display_details(self):
# print("{} {}".format(self.company, self.model))
#
# my_car = Car("Maruti", "Ertiga")
# my_car.display_details()
# 3.Inheritance
# Create an ElectricCar class that inherits from the Car class and has an
# additional attribute battery_size.
# class Car:
# def __init__(self, company,model):
# self.company = company
# self.model = model
#
# def display_details(self):
# print("{} {}".format(self.company, self.model))
#
# class ElectricCar(Car):
# def __init__(self, brand, model, battery_size):
# super().__init__(brand, model)
# self.battery_size = battery_size
#
# my_car = ElectricCar('Tesla', 'Model S', '85KwH')
# print(my_car.model)
# my_car.display_details()
# we have access of all the methods & attributes of Car class
# 4.Encapsulation
# Modify the Car class to encapsulate the brand attribute, making it private, and
# provide a getter method for it
# class Car:
# def __init__(self, brand,model):
# self.__brand = brand
# self.model = model
#
# def display_details(self):
# print("{} {}".format(self.__brand, self.model))
#
# # getter method
# def get_brand(self):
# return self.__brand +" !"
#
# # setter method
# def set_brand(self, new_brand):
# self.__brand = new_brand
#
# class ElectricCar(Car):
# def __init__(self, brand, model, battery_size):
# super().__init__(brand, model)
# self.battery_size = battery_size
#
# my_car = ElectricCar('Tesla', 'Model S', '85KwH')
# print(my_car.__brand)
# gives error
# __brand is now private attribute
# print(my_car.get_brand())
# my_car.set_brand("Ferrari")
# print("New car brand is {}.".format(my_car.get_brand()))
# 5.Polymorphism
# demonstrate the polymorphism by defining a method fuel_type in both Car &
# ElectricCar classes but with different behaviours.
# class Car:
# def __init__(self, brand,model):
# self.__brand = brand
# self.model = model
#
# def display_details(self):
# print("{} {}".format(self.__brand, self.model))
#
# # getter method
# def get_brand(self):
# return self.__brand +" !"
#
# # setter method
# def set_brand(self, new_brand):
# self.__brand = new_brand
#
# def fuel_type(self):
# return "Petrol or diesel"
#
# class ElectricCar(Car):
# def __init__(self, brand, model, battery_size):
# super().__init__(brand, model)
# self.battery_size = battery_size
#
# def fuel_type(self):
# return "Electric charge."
#
# my_car = ElectricCar('Tesla', 'Model S', '85KwH')
# my_new_car = Car("Toyota", "Corolla")
# print("my car has {} fuel type.".format(my_car.fuel_type()))
# print("my new car has {} fuel type.".format(my_new_car.fuel_type()))
# 6.class variables
# Add a class to Car that keeps track of the number of cars reated.
# class Car:
# total_car = 0
# def __init__(self, brand,model):
# self.__brand = brand
# self.model = model
# Car.total_car +=1
#
# def display_details(self):
# print("{} {}".format(self.__brand, self.model))
#
# # getter method
# def get_brand(self):
# return self.__brand +" !"
#
# # setter method
# def set_brand(self, new_brand):
# self.__brand = new_brand
#
# def fuel_type(self):
# return "Petrol or diesel"
#
# class ElectricCar(Car):
# def __init__(self, brand, model, battery_size):
# super().__init__(brand, model)
# self.battery_size = battery_size
#
# def fuel_type(self):
# return "Electric charge."
#
# my_car = ElectricCar('Tesla', 'Model S', '85KwH')
# my_new_car = Car("Toyota", "Corolla")
#
# # use this 'Car' class variable
# print("total car created {}".format(Car.total_car))
# note:there is no immediate garbage collection in python.
# 7.static method
# Add a static method to the Car class that returns a general description of a
# Car.
# class Car:
# total_car = 0
# def __init__(self, brand,model):
# self.__brand = brand
# self.model = model
# Car.total_car +=1
#
# def display_details(self):
# print("{} {}".format(self.__brand, self.model))
#
# # getter method
# def get_brand(self):
# return self.__brand +" !"
#
# # setter method
# def set_brand(self, new_brand):
# self.__brand = new_brand
#
# def fuel_type(self):
# return "Petrol or diesel"
#
# # @staticmethod is called decorators
# @staticmethod
# def general_description():
# return 'Cars are means for transport.'
#
# class ElectricCar(Car):
# def __init__(self, brand, model, battery_size):
# super().__init__(brand, model)
# self.battery_size = battery_size
#
# def fuel_type(self):
# return "Electric charge."
#
#
# my_car = ElectricCar('Tesla', 'Model S', '85KwH')
# my_new_car = Car("Toyota", "Corolla")
#
# # print(my_car.general_description())
# # error
#
# print(Car.general_description())
# 8.Property decorator
# Use a property decorator in the Car class to make the model attribute read-
# -only
# class Car:
# total_car = 0
# def __init__(self, brand,model):
# self.__brand = brand
# self.__model = model
# Car.total_car +=1
#
# def display_details(self):
# print("{} {}".format(self.__brand, self.__model))
#
# # getter method
# def get_brand(self):
# return self.__brand +" !"
#
# # setter method
# def set_brand(self, new_brand):
# self.__brand = new_brand
#
# def fuel_type(self):
# return "Petrol or diesel"
#
# # @staticmethod is called decorators
# @staticmethod
# def general_description():
# return 'Cars are means for transport.'
#
# @property
# def model(self):
# return self.__model
#
# class ElectricCar(Car):
# def __init__(self, brand, model, battery_size):
# super().__init__(brand, model)
# self.battery_size = battery_size
#
# def fuel_type(self):
# return "Electric charge."
#
#
# # my_car = ElectricCar('Tesla', 'Model S', '85KwH')
# # my_car.model = "City"
# # error
#
# my_new_car = Car("Toyota", "Corolla")
#
# print(my_car.model)
# 9.Class inheritance & isinstance() Function
# Demonstrate the use of isinstance() to check if my_tesla is an instance of Car
# and ElectricCar
# class Car:
# total_car = 0
# def __init__(self, brand,model):
# self.__brand = brand
# self.__model = model
# Car.total_car +=1
#
# def display_details(self):
# print("{} {}".format(self.__brand, self.__model))
#
# # getter method
# def get_brand(self):
# return self.__brand +" !"
#
# # setter method
# def set_brand(self, new_brand):
# self.__brand = new_brand
#
# def fuel_type(self):
# return "Petrol or diesel"
#
# # @staticmethod is called decorators
# @staticmethod
# def general_description():
# return 'Cars are means for transport.'
#
# @property
# def model(self):
# return self.__model
#
# class ElectricCar(Car):
# def __init__(self, brand, model, battery_size):
# super().__init__(brand, model)
# self.battery_size = battery_size
#
# def fuel_type(self):
# return "Electric charge."
#
#
# my_car = ElectricCar('Tesla', 'Model S', '85KwH')
#
# print(isinstance(my_car, Car))
# Multiple inheritance
# 10.Create two classes. Battery and Engine, and let the Electric class inherit
# from both, demonstrating multiple inheritance.
# class Car:
# total_car = 0
# def __init__(self, brand,model):
# self.__brand = brand
# self.__model = model
# Car.total_car +=1
#
# def display_details(self):
# print("{} {}".format(self.__brand, self.__model))
#
# # getter method
# def get_brand(self):
# return self.__brand +" !"
#
# # setter method
# def set_brand(self, new_brand):
# self.__brand = new_brand
#
# def fuel_type(self):
# return "Petrol or diesel"
#
# # @staticmethod is called decorators
# @staticmethod
# def general_description():
# return 'Cars are means for transport.'
#
# @property
# def model(self):
# return self.__model
#
# class ElectricCar(Car):
# def __init__(self, brand, model, battery_size):
# super().__init__(brand, model)
# self.battery_size = battery_size
#
# def fuel_type(self):
# return "Electric charge."
#
# # note: pass keyword is a placeholder, which is used to create an empty class.
# class Battery:
# def battery_info(self):
# return "This is battery"
#
# class Engine:
# def engine_info(self):
# return "This is an engine."
#
# class EleCarTwo(Battery, Engine, Car):
# pass
#
# my_new_tela = EleCarTwo("Tesla", "Models S")
# print(my_new_tela.engine_info())
# print(my_new_tela.battery_info())
# # code is working