-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass&subclass
114 lines (77 loc) · 2.69 KB
/
class&subclass
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
import random
## (SELF,NAME) name is something that you need to put
class cube(object):
def __init__(self,name):
self.name=name
def query(self):
return('I am a cube, my name is: ' + self.name)
class dimension():
def __init__(self):
self.width =20
self.height =20
class colorwithdimension(cube,dimension):
def __init__(self,name):
self.color = random.choice(['BLUE', 'RED', 'PURPLE'])
cube.__init__(self,name)
dimension.__init__(self)
def Query(self):
return super().query()+ (' my color is: ' + self.color ) + '\n' + ('My Dimensions are: WIDTH:' +
str(self.width) + ' HEIGHT:' + str(self.height))
colorcube1= colorwithdimension('BOB')
print(colorcube1.Query())
class ChristmasTree:
def __init__(self):
self.decorations = []
def add_decoration(self,decoration):
self.decorations.append(decoration)
def count_by_color(self,color):
count=0
for dec in self.decorations:
if dec.color == color :
count= count+1
return count
class Decoration:
def __init__(self,color,shape):
self.color = color
self.shape = shape
ob1=Decoration(color='R', shape ='C')
ob2=Decoration(color='R', shape='C')
ob3=Decoration(color='B', shape='C')
tree= ChristmasTree()
tree.add_decoration(ob1)
tree.add_decoration(ob2)
tree.add_decoration(ob3)
print(tree.count_by_color('R'))
class Contact:
all_contacts=[]
def __init__(self,name,email):
self.name= name
self.email =email
Contact.all_contacts.append(self)
class Supplier(Contact):
def order(self,order):
print('If this were a real system we would send {} oder to {}'.format(order,self.name))
c = Contact("Some Body", "[email protected]")
s = Supplier("Sup Plier", "[email protected]")
s.order("I need pliers")
class ContactList(list):
def search(self,name):
matching_contacts=[]
# self(contact)==contact==contact.name
for contact in self:
## WILL RETURN A LIST OF OBJECT IF YOU WANNA THE NAME ONLY
if name in contact.name:
matching_contacts.append(contact)
return matching_contacts
class Contact:
all_contacts= ContactList()
def __init__(self,name,email):
self.name = name
self.email = email
self.all_contacts.append(self)
c1 = Contact("John A", "[email protected]")
c2 = Contact("John B", "[email protected]")
c3 = Contact("Jenna C", "[email protected]")
print(Contact.all_contacts.search('John'))
a=[contact.name for contact in Contact.all_contacts.search('John') ]
print(a)