-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathoop08 (combining multiple classes).py
More file actions
38 lines (30 loc) · 1.04 KB
/
oop08 (combining multiple classes).py
File metadata and controls
38 lines (30 loc) · 1.04 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
# oop_extra 8
# combining multiple classes and object.
class Robot :
def __init__(self,name,color,weight):
self.name=name
self.color=color
self.weight=weight
def introduce_self(self):
return "My name is "+ self.name
r1=Robot("Tom","red",30)
r2=Robot("Jerry","blue",40)
print(r1.introduce_self())
print(r2.introduce_self())
class Person :
def __init__(self,name,personality,isSitting):
self.name=name
self.personality=personality
self.isSitting=isSitting
def sit_down(self):# when we run this method to any object the is sitting value will be true.
self.isSitting=True
def stand_up(self):# when we run this method to any object the is sitting value will be false.
self.isSitting=False
p1=Person("Shawki","Intelligent",False)
p2=Person("Sowad","talkative",True)
# if p1 owns r2 and p2 owns r1
p1.robotOwened=r2
p2.robotOwened=r1
# now we can access this robotOwned atrribute in p1/p2 object.
print(p1.robotOwened.introduce_self())
print(p2.robotOwened.introduce_self())