23面向对象案例1

目录

1、计算连续表达式的一个过程

2、优化后的代码

[为什么不能return result+=n?](#为什么不能return result+=n?)

3、用面向对象的方法可以解决冗余的问题,但是还是不能解决result的值可以被随意修改的问题

4、解决不能被随意修改的问题,可以将类属性改成私有变量吗,但是随之而来的问题就是也不能更改读取和访问了

[5、最后用实例对象极限优化,。。我不能理解 为啥一开始不用实例对象来着,不是更容易吗](#5、最后用实例对象极限优化,。。我不能理解 为啥一开始不用实例对象来着,不是更容易吗)


1、计算连续表达式的一个过程

但是这个代码有点冗余,有一点可以修改和优化的地方,具体看如下

python 复制代码
#计算器,实现一些基本操作,如:加减乘除
def jia(n1,n2):
    return n1 +n2
def jian(n1,n2):
    return n1-n2
def cheng(n1,n2):
    return n1*n2
def chu(n1,n2):
    return n1 / n2
# res = jia(3,4)
# print(res)
# res1 = jian(3,8)
# print(res1)
#(2+6-4)*5
r1 = jia(2,6)
r2 = jian(r1,4)
r3 = cheng(r2,5)
print(r3)

2、优化后的代码

python 复制代码
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
def chu(n):
    global result
    result / n
# res = jia(3,4)
# print(res)
# res1 = jian(3,8)
# print(res1)
#(2+6-4)*5
# r1 = jia(2,6)
# r2 = jian(r1,4)
# r3 = cheng(r2,5)
# print(r3)

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

为什么不能return result+=n?

在Python中,return 语句用于从函数中返回一个值,并且结束函数的执行。而 result += n 是一个赋值语句,它将 result 的值加上 n 并赋给 result,但它并不返回任何值。

3、用面向对象的方法可以解决冗余的问题,但是还是不能解决result的值可以被随意修改的问题

python 复制代码
class Caculator:
    result = 0
    @classmethod
    def first_value(cls,n):
        cls.result = n
    @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 chu(cls,n):
        cls.result /= n
Caculator.first_value(2)
Caculator.jia(6)
Caculator.jian(4)
Caculator.cheng(5)
print(Caculator.result)

4、解决不能被随意修改的问题,可以将类属性改成私有变量吗,但是随之而来的问题就是也不能更改读取和访问了

python 复制代码
class Caculator:
    __result = 0
    @classmethod
    def first_value(cls,n):
        cls.__result = n
    @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 chu(cls,n):
        cls.__result /= n
    @classmethod
    def show(cls):
        print('计算的结果是%s'%cls.__result)
Caculator.first_value(2)
Caculator.jia(6)
Caculator.jian(4)
Caculator.cheng(5)
Caculator.show()

改成私有属性,照样可以访问!!!只不过需要在最后面那块加一个显示函数类似于show这种的~~~

5、最后用实例对象极限优化,。。我不能理解 为啥一开始不用实例对象来着,不是更容易吗

python 复制代码
class Caculor():
    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 chu(self,n):
        self.__result /= n
    def show(self):
        self.__result
        print('最终计算结果是%s'%self.__result)
p1 = Caculor(2)

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

6、更好的修改是增加一个容错机制

python 复制代码
class Caculator:
    def __init__(self,num):
        if not isinstance(num,int):
            raise TypeError('阿偶,不是一个整型数据哦')
        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 chu(self,n):
        self.__result /= n
    def show(self):
        print('最终结果是%s'%self.__result)
c1 = Caculator('axc')
c1.jia(6)
c1.jian(4)
c1.cheng(5)
c1.show()
相关推荐
wdfk_prog1 分钟前
lwip教程 38:从 `lwipopts.h` 到 Port Contract——lwIP 裁剪、构建选择、OS Port 与 Network Port
java·开发语言
开开心心就好4 分钟前
证件照排版打印工具,免费可自定义尺寸
服务器·开发语言·算法·随机森林·pdf·音视频·散列表
方知我7 分钟前
01FastAPI基础入门
人工智能·python
我是高手高手高高手10 分钟前
宝塔 PHP多版本,切换PHP默认版本
开发语言·php
CodeYp201911 分钟前
第16讲:set 和 map 的使用
开发语言·c++·set·map
物联网老王12 分钟前
第05篇-Agent 连不上工具?MCP 就是那根“USB-C”
开发语言·人工智能·ai·aigc·ai编程
DreamLife☼27 分钟前
Agent与大语言模型的集成
windows·python·语言模型
BK贝壳6 小时前
亿级用户签到位图选型复盘:用 bool-hybrid-array 扛冷启动打卡,也把它和 Roaring/Redis 的边界测清楚了
数据结构·python·性能优化·numpy
2601_966949657 小时前
如何根据研究的起止时间截取分钟级行情?Python 实战与 QuantDash 数据处理指南
开发语言·python·pandas·量化交易·1024程序员节·股票数据·quantdash
晨曦中的暮雨9 小时前
限流算法——固定窗口、滑动窗口、漏桶、令牌桶
java·开发语言·算法