python---面向对象---综合案例(4)

案例描述

实现加减乘法运算

python 复制代码
# _*_ encoding:utf-8 _*_

# 计算器, 实现一些基本的操作, 加减乘除运算, 以及打印结果操作

# ------------------------------------代码1--------------------------------------
def jia(n1, n2):
    return n1 + n2

def jian(n1, n2):
    return n1 - n2

def cheng(n1, n2):
    return n1 * n2

res = jia(2, 4)
res2 = cheng(5, 7)
print(res)
print(res2)

#(2 + 6 - 4) * 5
r1 = jia(2, 6)
r2 = jian(r1, 4)
r3 = cheng(r2, 5)
print(r3)

# ------------------------------------代码2--------------------------------------
result = 0

def first_value(v):
    global result
    result = v

def jia(n):
    global result
    result += n

def jian(n):
    global result
    result -= n

def cheng(n):
    global result
    result *= n


first_value(2)
jia(6)
result = 123
jian(4)
cheng(5)
print(result)

# ------------------------------------代码3--------------------------------------
## 封装到类,提高安全性
class Caculator:
    __result = 0

    @classmethod
    def first_value(cls, v):
        cls.__result = v

    @classmethod
    def jia(cls, n):
        cls.__result += n

    @classmethod
    def jian(cls, n):
        cls.__result -= n

    @classmethod
    def cheng(cls, n):
        cls.__result *= n

    @classmethod
    def show(cls):
        print("计算的结果是:%d" % cls.__result)

Caculator.first_value(2)

Caculator.jia(6)
Caculator.jian(4)
Caculator.cheng(5)
Caculator.show()

# ------------------------------------代码4--------------------------------------
## 增加并发功能
class Caculator:

    def __init__(self, num):
        self.__result = num

    def jia(self, n):
        self.__result += n

    def jian(self, n):
        self.__result -= n

    def cheng(self, n):
        self.__result *= n

    def show(self):
        print("计算的结果是:%d" % self.__result)
# 创建计算器
c1 = Caculator(2)
# 运算
c1.jia(6)
c1.jian(4)
c1.cheng(5)
c1.show()

# ------------------------------------代码5--------------------------------------
## 容错处理,数据验证
class Caculator:
    def check_num(self, num):
        if not isinstance(num, int):
            raise TypeError("当前这个数据的类型有问题, 应该是一个整型数据")
            
    def __init__(self, num):
        self.check_num(num)
        self.__result = num

    def jia(self, n):
        self.check_num(n)
        self.__result += n

    def jian(self, n):
        self.check_num(n)
        self.__result -= n

    def cheng(self, n):
        self.check_num(n)
        self.__result *= n

    def show(self):
        print("计算的结果是:%d" % self.__result)

c1 = Caculator(2)
c1.jia(6)
c1.jian("a")
c1.cheng(5)
c1.show()

# ------------------------------------代码6--------------------------------------
# 使用装饰器进行优化重构,维护代码的完整性
class Caculator:
    def check_num_zsq(func):
        def inner(self, n):
            if not isinstance(n, int):
                raise TypeError("当前这个数据的类型有问题, 应该是一个整型数据")
            return func(self, n)
        return inner

    @check_num_zsq
    def __init__(self, num):
        self.__result = num

    @check_num_zsq
    def jia(self, n):
        self.__result += n

    @check_num_zsq
    def jian(self, n):
        self.__result -= n

    @check_num_zsq
    def cheng(self, n):
        self.__result *= n

    def show(self):
        print("计算的结果是:%d" % self.__result)

c1 = Caculator(2)
c1.jia(6)
c1.jian(4)
c1.cheng(5)
c1.show()

# ------------------------------------代码7--------------------------------------
# 实例调用类中的装饰器会报错,将装饰器设为私有
class Caculator:
    def __check_num_zsq(func):
        def inner(self, n):
            if not isinstance(n, int):
                raise TypeError("当前这个数据的类型有问题, 应该是一个整型数据")
            return func(self, n)
        return inner

    @__check_num_zsq
    def __init__(self, num):
        self.__result = num

    @__check_num_zsq
    def jia(self, n):
        self.__result += n

    @__check_num_zsq
    def jian(self, n):
        self.__result -= n

    @__check_num_zsq
    def cheng(self, n):
        self.__result *= n

    def show(self):
        print("计算的结果是:%d" % self.__result)

c1 = Caculator(2)
c1.jia(6)
c1.jian(4)
c1.cheng(5)
c1.show()

# ------------------------------------代码8--------------------------------------
# 针对用户的每个操作,进行语音播报,类似开启语音播报的计算器

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):
        # 1. 创建一个播报器对象
        speaker = win32com.client.Dispatch("SAPI.SpVoice")
    
        # 2. 通过这个播报器对象, 直接, 播放相对应的语音字符串就可以
        speaker.Speak(word)

    def create_say_zsq(word=""):
        def __say_zsq(func):
            def inner(self, n):
                # 1. 创建一个播报器对象
                speaker = win32com.client.Dispatch("SAPI.SpVoice")
                # 2. 通过这个播报器对象, 直接, 播放相对应的语音字符串就可以
                speaker.Speak(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, n):
        self.__result += n

    @__check_num_zsq
    @create_say_zsq("减去")
    def jian(self, n):
        self.__result -= n

    @__check_num_zsq
    @create_say_zsq("乘以")
    def cheng(self, n):
        self.__result *= n

    def show(self):
        self.__say("计算的结果是:%d" % self.__result)
        print("计算的结果是:%d" % self.__result)

c1 = Caculator(10)
c1.jia(6)
c1.jian(4)
c1.cheng(5)
c1.show()

# ------------------------------------代码9--------------------------------------
# 播报优化
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):
        # 1. 创建一个播报器对象
        speaker = win32com.client.Dispatch("SAPI.SpVoice")

        # 2. 通过这个播报器对象, 直接, 播放相对应的语音字符串就可以
         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, n):
        self.__result += n

    @__check_num_zsq
    @__create_say_zsq("减去")
    def jian(self, n):
        self.__result -= n

    @__check_num_zsq
    @__create_say_zsq("乘以")
    def cheng(self, n):
        self.__result *= n

    def show(self):
        self.__say("计算的结果是:%d" % self.__result)
        print("计算的结果是:%d" % self.__result)

c1 = Caculator(10)
c1.jia(6)
c1.jian(4)
c1.cheng(5)
c1.show()


# ------------------------------------代码10--------------------------------------
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):
        # 1. 创建一个播报器对象
        speaker = win32com.client.Dispatch("SAPI.SpVoice")

        # 2. 通过这个播报器对象, 直接, 播放相对应的语音字符串就可以
        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, n):
        self.__result += n

    @__check_num_zsq
    @__create_say_zsq("减去")
    def jian(self, n):
        self.__result -= n

    @__check_num_zsq
    @__create_say_zsq("乘以")
    def cheng(self, n):
        self.__result *= n

    def show(self):
        self.__say("计算的结果是:%d" % self.__result)
        print("计算的结果是:%d" % self.__result)

    @property
    def result(self):
        return self.__result

c1 = Caculator(10)
c1.jia(6)
c1.jian(4)
c1.cheng(5)
c1.show()

print(c1.result)
c1.result = 10

# ------------------------------------代码11--------------------------------------
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):
        # 1. 创建一个播报器对象
        speaker = win32com.client.Dispatch("SAPI.SpVoice")

        # 2. 通过这个播报器对象, 直接, 播放相对应的语音字符串就可以
        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, n):
        self.__result += n
        return self

    @__check_num_zsq
    @__create_say_zsq("减去")
    def jian(self, n):
        self.__result -= n
        return self

    @__check_num_zsq
    @__create_say_zsq("乘以")
    def cheng(self, n):
        self.__result *= n
        return self

    def show(self):
        self.__say("计算的结果是:%d" % self.__result)
        print("计算的结果是:%d" % self.__result)
        return self

    def clear(self):
        self.__result = 0
        return self

    @property
    def result(self):
        return self.__result

c1 = Caculator(10)
c1.jia(6).jian(4).cheng(5).show().clear().jia(555).jian(500).show() # 链式编程,在方法内部再次返回调用方法的实例本身

print(c1.result)


# ------------------------------------代码12--------------------------------------


import win32com.client

class Caculator(object):
    @staticmethod
    def __say(word):
        speaker = win32com.client.Dispatch("SAPI.SpVoice")
        speaker.Speak(word)

    def createSayZSQ(operation=""):
        def say(func):
            def inner(self, v):
                Caculator.__say(operation + str(v))
                return func(self, v)
            return inner
        return say


    def __check_num(func):
        def inner(self, v):
            if not isinstance(v, int):
                raise TypeError("数据类型有误, 应该是一个整型数据")
            return func(self, v)
        return inner

    @createSayZSQ()
    @__check_num
    def __init__(self, v):
        self.__result = v

    @createSayZSQ("加")
    @__check_num
    def jia(self, n):
        self.__result += n
        return self

    @createSayZSQ("减")
    @__check_num
    def jian(self, n):
        self.__result -= n
        return self

    @createSayZSQ("乘")
    @__check_num
    def cheng(self, n):
        self.__result *= n
        return self

    def show(self):
        self.__say("计算结果为:%d" % self.__result)
        print("计算结果为:%d" % self.__result)
        return self

c = Caculator(2)
c.jia(6).jian(4).cheng(3).show()

补充

使用Python调用微软的语音接口
	import win32com.client
	speaker = win32com.client.Dispatch("SAPI.SpVoice")
	speaker.Speak(word)
相关推荐
一只大侠2 分钟前
输入一串字符,以“?”结束。统计其中字母个数,数字个数,其它符号个数。:JAVA
java·开发语言·算法
Oneforlove_twoforjob3 分钟前
【Java基础面试题011】什么是Java中的自动装箱和拆箱?
java·开发语言
优雅的落幕20 分钟前
多线程---线程安全(synchronized)
java·开发语言·jvm
小黄编程快乐屋22 分钟前
前端小练习——大雪纷飞(JS没有上限!!!)
开发语言·前端·javascript
爱上语文26 分钟前
请求响应:常见参数接收及封装(数组集合参数及日期参数)
java·开发语言·spring boot·后端
清风徐来辽37 分钟前
Kotlin学习:1.7.语言基础之空安全
开发语言·kotlin
灵封~37 分钟前
PythonQt练习
python
CQU_JIAKE39 分钟前
926[study]Docker,DHCP
java·开发语言
程序猿进阶43 分钟前
Tomcat 都有哪些核心组件
java·开发语言·后端·面试·性能优化·tomcat·firefox
猫猫的小茶馆1 小时前
【Linux系统】Linux内核框架(详细版本)
linux·运维·服务器·开发语言·嵌入式软件