-
Notifications
You must be signed in to change notification settings - Fork 0
/
oops.py
154 lines (115 loc) · 3.01 KB
/
oops.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
from curses.textpad import rectangle
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def start(self):
print('The car is starting...')
def drive(self):
print('The car is driving...')
def stop(self):
print('The car is stopping...')
# Inheritance
class ElectricCar(Car):
def __init__(self, make, model, year, battery_capacity):
super().__init__(make,model,year)
self.battery_capacity = battery_capacity
def charge(self):
print('The car is charging...')
# Encapsulation
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
def get_name(self):
return self._name
def set_name(self, name):
self._name = name
def get_age(self):
return self._age
def set_age(self, age):
if 0 <= age <= 120:
self._age = age
else:
print("Invalid age")
# Polymorphism
class Animal:
def make_sound(self):
print('Some generic Animal Sound')
class Dog(Animal):
def make_sound(self):
print('Bark!')
class Cat(Animal):
def make_sound1(self):
print('Meow!')
# Abstraction
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
class Rectangle(Shape):
def __init__(self, length, breadth):
self.length = length
self.breadth = breadth
def area(self):
return self.length * self.breadth
# Aggregation
class Book:
def __init__(self, title):
self.title = title
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
# Composition
class Room:
def __init__(self, name):
self.name = name
class House:
def __init__(self):
self.rooms = [Room("Living Room"), Room("Bedroom")]
if __name__ == '__main__':
car = Car("Toyota", "Camry", 2014)
car.start()
car.drive()
car.stop()
print()
my_electric_car = ElectricCar("Tesla", "Model S", 2022, 100)
my_electric_car.start()
my_electric_car.drive()
my_electric_car.stop()
my_electric_car.charge()
print()
person = Person("Ravi", 28)
print(person.get_name())
print(person.get_age())
person.set_age(30)
print(person.get_age())
print()
dog = Dog()
cat = Cat()
dog.make_sound()
cat.make_sound()
print()
circle = Circle(2)
rectangle = Rectangle(2, 3)
print(f'Area of circle is {circle.area()}')
print(f'Area of rectangle is {rectangle.area()}')
print()
library = Library()
library.add_book(Book("Book1"))
library.add_book(Book("Book2"))
for book in library.books:
print(book.title)
print()
house = House()
for room in house.rooms:
print(room.name)