计算器,实现一些基本的操作,加减乘除运算,以及打印结果操作
点击查看代码
import win32com.client
class Caculator:
# 装饰器私有
def __check_num_zsq(func):
def inner(self, n):
if not isinstance(n, int):
raise TypeError("当前这个数据的类型有问题,应该是一个整型数据")
return func(self, n)
return inner
def __say(self, word):
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak(word)
def __create_say_zsq(word=""):
def __say_zsq(func):
def inner(self, n):
self.__say(word + str(n))
return func(self, n)
return inner
return __say_zsq
@__check_num_zsq
@__create_say_zsq()
def __init__(self, num):
self.__result = num
@__check_num_zsq
@__create_say_zsq("加")
def jia(self, num):
self.__result += num
return self
@__check_num_zsq
@__create_say_zsq("减去")
def jian(self, num):
self.__result -= num
return self
@__check_num_zsq
@__create_say_zsq("乘以")
def cheng(self, num):
self.__result *= num
return self
def show(self):
print("计算结果: %d" % self.__result)
self.__say("计算结果: %d" % self.__result)
return self
def clear(self):
self.__result = 0
return self
@property
def result(self):
return self.__result
c1 = Caculator(2)
c1.jia(6).jian(4).cheng(5).show().jia(555).show()
print(c1.result)
学习链接: 【Python】零基础入门 面向对象编程(强烈推荐)
标签:__,return,zsq,python,self,笔记,面向对象,num,def From: https://www.cnblogs.com/amanzhj/p/17809866.html