Skip to content

Commit 442a568

Browse files
authored
added aggregation and how to create module
1 parent 0fce51c commit 442a568

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
from rectangle import Rectangle
4+
from triangle import Triangle
5+
6+
rec = Rectangle()
7+
tri = Triangle()
8+
rec.set_values(50, 60)
9+
tri.set_values(10, 45)
10+
print(rec.area())
11+
print(tri.area())
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Polygon:
2+
3+
__width = None
4+
__height = None
5+
6+
def set_values(self, width, height):
7+
self.__width = width
8+
self.__height = height
9+
10+
def get_width(self):
11+
return self.__width
12+
def get_height(self):
13+
return self.__height
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
from polygon import Polygon
4+
5+
class Rectangle(Polygon):
6+
def area(self):
7+
return self.get_width() * self.get_height()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
from polygon import Polygon:
3+
4+
5+
class Triangle(Polygon):
6+
def area(self):
7+
return self.get_width() * self.get_height() / 2
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# module is nothing but a python file
3+
4+
#from dir import imp --1st u caan also use as in this line # this code will auto appear after shifting the python file i.e. imp.py into new directory
5+
6+
# above same code function can done with simple way of 1 line which is below
7+
8+
9+
import dir.imp as rahul # as keyword for using short notation without as it was print((dir.imp.add(10,20)))
10+
11+
print((rahul.add(56, 15)))
12+
print((rahul.mul(56, 15)))
13+
14+
15+
16+
# print((imp.add(56, 15))) --1st
17+
# print((imp.mul(46, 57))) --1st
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
def add(a ,b):
4+
return a+b
5+
6+
def mul(a, b):
7+
return a * b

Beginner_Level/aggregation.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# COMPOSITION HAS relation of 'PART - OF ' #
3+
# AGGREGATION HAS relation of 'HAS - A' #
4+
5+
class Salary:
6+
7+
def __init__(self, pay, bonus):
8+
self.pay = pay
9+
self.bonus = bonus
10+
11+
def annual_salary(self):
12+
return (self.pay*12) + self.bonus
13+
14+
class Employee:
15+
def __init__(self, name, age , salary):
16+
self.name= name
17+
self.age= age
18+
self.obj_salary = salary;
19+
20+
def total_salary(self):
21+
return self.obj_salary.annual_salary()
22+
23+
24+
salary = Salary(15000, 10000)
25+
emp = Employee('easynamila', 25, salary) # Its unidirectional only salary is passing not name and age # don't forget to pass salary object
26+
27+
print(emp.total_salary())

0 commit comments

Comments
 (0)