2018-12-28
阅读量:
743
pyhton构造函数示例
构造函数声明的语法:
def __init __(self):
#构造函数的主体
参数化构造函数的示例:
class Addition:
first = 0
second = 0
answer = 0
# parameterized constructor
def __init__(self, f, s):
self.first = f
self.second = s
def display(self):
print("First number = " + str(self.first))
print("Second number = " + str(self.second))
print("Addition of two numbers = " + str(self.answer))
def calculate(self):
self.answer = self.first + self.second
# creating object of the class
# this will invoke parameterized constructor
obj = Addition(1000, 2000)
# perform Addition
obj.calculate()
# display result
obj.display()
输出:
First number =1000
Second number =2000
Addition of two numbers =3000






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论