-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass_exemple.py
More file actions
88 lines (60 loc) · 1.42 KB
/
class_exemple.py
File metadata and controls
88 lines (60 loc) · 1.42 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
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
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 values : ()".format(self.data))
z = SecondClass()
z.setdata(456)
# creation d'une classe
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()
# heritage
class SecondClass(FirstClass):
# overloading display
def display(self):
print("current value: {}".format(self.data))
z = SecondClass()
z.setdata(444)
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
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()
print(a)
b = a + 'xyz'
b.display()
print(b)
print(b)