AS
Python
Classi
Codice
class myClass:
"This is a class"
running_total = 0
def __init__(self, operator1=0, operator2=0):
self.x = operator1
self.y = operator2
def myMethod(self, operation="sum"):
self.o = operation
if self.o == "sum":
self.result = self.x + self.y
self.running_total = self.running_total + self.result
return self.result
elif self.o == "multiplication":
self.result = self.x * self.y
self.running_total = self.running_total + self.result
return self.result
def squareOperators(self):
self.x = self.x * self.x
self.y = self.y * self.y
myObj = myClass(2,3)
print(myObj.__doc__)
print("Runnning total: ", myObj.running_total)
print("Sum: ", myObj.myMethod())
print("Multiplication: ", myObj.myMethod("multiplication"))
print("Runnning total: ", myObj.running_total)
myObj.squareOperators()
print("Sum of squared: ", myObj.myMethod())
print("Runnning total: ", myObj.running_total)
Output
This is a class
Runnning total: 0
Sum: 5
Multiplication: 6
Runnning total: 11
Sum of squared: 13
Runnning total: 24