overloading 1
This commit is contained in:
parent
a10503c12d
commit
103edfb801
1 changed files with 28 additions and 0 deletions
28
python-temel/overloading.py
Normal file
28
python-temel/overloading.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
class Number:
|
||||
def __init__(self, number):
|
||||
self.number = number
|
||||
|
||||
def disp(self):
|
||||
print(self.number)
|
||||
|
||||
def __add__(self, x):
|
||||
return Number(self.number + x.number)
|
||||
|
||||
def __sub__(self, x):
|
||||
return Number(self.number - x.number)
|
||||
|
||||
def __mul__(self, x):
|
||||
return Number(self.number * x.number)
|
||||
|
||||
def __truediv__(self, x):
|
||||
return Number(self.number / x.number)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.number)
|
||||
|
||||
x = Number(10)
|
||||
y = Number(20)
|
||||
z = Number(2)
|
||||
|
||||
k = x + y * z
|
||||
print(k)
|
Loading…
Reference in a new issue