-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass_example.py
More file actions
44 lines (34 loc) · 803 Bytes
/
class_example.py
File metadata and controls
44 lines (34 loc) · 803 Bytes
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
class FirstClass:
def setdata(self, value):
self.data = value
def display(self):
print(self.data)
x = FirstClass()
y = FirstClass()
x.setdata("Thomas")
y.setdata(3.1415)
x.display()
y.display()
class SecondClass(FirstClass):
def display(self):
print("Current value : {}".format(self.data))
z = SecondClass()
z.setdata(456)
z.display()
class ThirdClass(SecondClass):
def __init__(self,value):
self.data = value
def __add__(self, other):
return ThirdClass(self.data + other)
def __str__(self):
return '[ThirdClass : {} ]'.format(self.data)
def mul(self,other):
self.data *= other
a = ThirdClass('abc')
a.display()
# Le print correspond à str
print(a)
# Le + correspond à add
b = a + 'xyz'
b.display()
print(b)