-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.py
More file actions
62 lines (45 loc) · 1.73 KB
/
oop.py
File metadata and controls
62 lines (45 loc) · 1.73 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
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def get_brand(self):
return self.brand
def full_name(self):
return f"{self.brand} {self.model}"
def full_type(self):
return "Petrol or Diesel"
@staticmethod
def general_description():
return "Cars are means of transport"
class ElectricCar(Car):
def __init__(self, brand, model, battery_size):
super().__init__(brand, model)
self.battery_size = battery_size
def full_type(self):
return "Electric Charge"
# Object creation and usage
my_tesla = ElectricCar("Tesla", "Model S", "85Kwh")
print(my_tesla.full_name()) # Tesla Model S
print(Car.general_description()) # Cars are means of transport
my_car = Car("Toyota", "Corolla")
print(my_car.brand) # Toyota
print(my_car.model) # Corolla
print(my_car.full_name()) # Toyota Corolla
my_new_car = Car("Tata", "Safari")
print(my_new_car.brand) # Tata
print(my_new_car.model) # Safari
print(isinstance(my_tesla, Car)) # True
print(isinstance(my_tesla, ElectricCar)) # True
# Multiple inheritance example
class Battery:
def battery_info(self):
return "This is a battery"
class Engine:
def engine_info(self):
return "This is an engine"
class ElectricCarTwo(Battery, Engine, Car):
def __init__(self, brand, model):
super().__init__(brand, model)
my_new_tesla = ElectricCarTwo("Tesla", "Model S")
print(my_new_tesla.engine_info()) # This is an engine
print(my_new_tesla.battery_info()) # This is a battery