forked from deepdalsania/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritanceEx2.py
More file actions
39 lines (30 loc) · 1.08 KB
/
InheritanceEx2.py
File metadata and controls
39 lines (30 loc) · 1.08 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
''' two different classes are realated, but there is no inheritance going
on. this is called composition where one class makes use of code contained
in another class'''
class Clothing:
stock = {'name': [], 'material': [], 'amount': []}
def __init__(self, name):
material = ""
self.name = name
def add_item(self, name, material, amount):
Clothing.stock['name'].append(self.name)
Clothing.stock['material'].append(self.material)
Clothing.stock['amount'].append(amount)
def Stock_by_Material(self, material):
count = 0
n = 0
for item in Clothing.stock['material']:
if item == material:
count += Clothing.stock['amount'][n]
n += 1
return count
class shirt(Clothing):
material = "Cotton"
class pants(Clothing):
material = "Cotton"
polo = shirt("Polo")
sweatpants = pants("Sweatpants")
polo.add_item(polo.name, polo.material, 4)
sweatpants.add_item(sweatpants.name, sweatpants.material, 6)
current_stock = polo.Stock_by_Material("Cotton")
print(current_stock)