前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Python零基础入门篇③④] - 私有函数、私有变量及封装

[Python零基础入门篇③④] - 私有函数、私有变量及封装

原创
作者头像
哈哥撩编程
发布2024-07-12 18:54:14
850
发布2024-07-12 18:54:14

通过学习私有函数与私有变量,可以更好的完善 “类” 的开发 ,从而丰满我们的场景与实现方案。

🍊 什么是私有函数和私有变量

私有函数与私有变量中的私有是什么意思? ---> 简单理解就是独自拥有、不公开、不分享的意思。放到函数与变量中就是独自拥有的函数与独自拥有的变量,并且不公开。这样我们就理解了什么是私有函数与私有变量。

  • 无法被实例化后的对象调用的类中的函数与变量
  • 虽然无法被实例化后的对象调用,但是在 类的内部 我们可以 调用私有函数与私有变量
  • 私有函数与私有变量的目的:只希望类内部的业务调用使用,不希望被实例化对象调用使用
  • 既然有私有函数与私有变量,其实能被实例化对象调用的函数与变量就是公有函数与公有变量,不过一般我们都称之为函数与变量。😓

🍊 私有函数与私有变量的定义方法

如何定义私有函数与私有变量:在 类变量 与 类函数 前添加__(2个下横线)即可定义私有函数与私有变量;变量或函数的后面无需添加,左右都有两个下横线,是 类的内置函数 的定义规范。

私有函数与私有变量示例如下:

代码语言:python
代码运行次数:0
复制
class Persion(object):
    
    def __init__(self):
        self.name = name
        self.__age = 18					# 'self.__age' 为 Persion类 私有变量
        
    def run(self):
        print(self.name, self.__age)	# 在  Persion类 的代码块中,私有变量依然可以被调用
        
    def __eat(self):					# '__eat(self)' 为 Persion类 私有函数
        return 'I want eat some fruits'    

接下来我们根据上面的示例代码做一下修改,更好的演示一下 私有函数与私有变量 方便加深理解

代码语言:python
代码运行次数:0
复制
class PersionInfo(object):

    def __init__(self, name):
        self.name = name

    def eat(self):
        result = self.__eat()
        print(result)

    def __eat(self):
        return f'{self.name} 最喜欢吃水果是 \'榴莲\' 和 \'番石榴\''

    def run(self):
        result = self.__run()
        print(result)

    def __run(self):
        return f'{self.name} 最喜欢的健身方式是 \'跑步\' 和 \'游泳\''


persion = PersionInfo(name='Neo')
persion.eat()
persion.run()

# >>> 执行结果如下:
# >>> Neo 最喜欢吃水果是 '榴莲' 和 '番石榴'
# >>> Neo 最喜欢的健身方式是 '跑步' 和 '游泳'

我们再试一下 通过 实例化对象 persion 调用 __eat 私有函数试试

代码语言:python
代码运行次数:0
复制
class PersionInfo(object):

    def __init__(self, name):
        self.name = name

    def eat(self):
        result = self.__eat()
        print(result)

    def __eat(self):
        return f'{self.name} 最喜欢吃水果是 \'榴莲\' 和 \'番石榴\''

    def run(self):
        result = self.__run()
        print(result)

    def __run(self):
        return f'{self.name} 最喜欢的健身方式是 \'跑步\' 和 \'游泳\''


persion = PersionInfo(name='Neo')
persion.__eat()

# >>> 执行结果如下:
# >>> AttributeError: 'PersionInfo' object has no attribute '__eat'
# >>> 再一次证明 实例化对象是不可以调用私有函数的

那么事实真的是 实例化对象就没有办法调用 私有函数 了么?其实不是的,我们继续往下看

代码语言:python
代码运行次数:0
复制
class PersionInfo(object):

    def __init__(self, name):
        self.name = name

    def eat(self):
        result = self.__eat()
        print(result)

    def __eat(self):
        return f'{self.name} 最喜欢吃水果是 \'榴莲\' 和 \'番石榴\''

    def run(self):
        result = self.__run()
        print(result)

    def __run(self):
        return f'{self.name} 最喜欢的健身方式是 \'跑步\' 和 \'游泳\''


persion = PersionInfo(name='Neo')

# 通过 dir() 函数 查看一下 实例化对象 persion 中都有哪些函数?

print(dir(persion))

<div align=center><img src="https://img-blog.csdnimg.cn/img_convert/141f43754d04c0ce0d74bd32aa061572.png" width="1500" ></div>


可以看到 实例化对象 persion 也有两个私有变量 _Persion__eat_Persion__run ,尝试直接用实例化对象 persion 调用私有变量。

代码语言:python
代码运行次数:0
复制
class PersionInfo(object):

    def __init__(self, name):
        self.name = name

    def eat(self):
        result = self.__eat()
        print(result)

    def __eat(self):
        return f'{self.name} 最喜欢吃水果是 \'榴莲\' 和 \'番石榴\''

    def run(self):
        result = self.__run()
        print(result)

    def __run(self):
        return f'{self.name} 最喜欢的健身方式是 \'跑步\' 和 \'游泳\''


persion = PersionInfo(name='Neo')

# 通过 dir() 函数 查看一下 实例化对象 persion 中都有哪些函数?

print(dir(persion))

print(persion._PersionInfo__eat())
print(persion._PersionInfo__run())

# >>> 执行结果如下图:

可以看到通过这种方式,我们的 实例化对象 persion 也成功的调用了 PersionInfo 类 的私有函数;但是既然是 私有函数 ,那么目的就是不希望被实例化对象调用,所以我们还是按照编码规范来使用比较好。


附:私有变量(私有属性)的使用与私有函数一样,我们看下面的示例

代码语言:python
代码运行次数:0
复制
class PersionInfo(object):
    __car = 'BMW'

    def __init__(self, name, sex):
        self.name = name
        self.__sex = sex

    def info(self):
        result = self.__info()
        print(result)

    def __info(self):
        return f'{self.name} 性别:{self.__sex} ,他有一辆:\'{self.__car}\''


persion = PersionInfo(name='Neo', sex='男')
persion.info()

# >>> 执行结果如下:
# >>> Neo 性别:男 ,他有一辆:'BMW'


# >>> 尝试调用私有函数		私有函数与变量(属性)['_PersionInfo__car', '_PersionInfo__info', '_PersionInfo__sex']
print(persion._PersionInfo__info())		

# >>> 执行结果如下:
# >>> Neo 性别:男 ,他有一辆:'BMW'

🍊 Python 中的封装

其实 Python 中并没有 封装 这个功能,而封装只是针对 Python 中某种业务场景的一种概念而已。

封装的概念 ---> 将不对外的私有属性或方法通过可以对外使用的函数而使用(类中定义的私有函数、私有方法只能在类的内部使用,外部无法访问),这样做的主要原因是:保护隐私,明确的区分内与外。

封装的示例如下:

代码语言:python
代码运行次数:0
复制
class Persion(object):
    
    def __hello(self, data):
        print('hello %s' % data)
        
    def helloworld(self):
        self.__hello('world')
        
        
if __name__ == '__main__':
	persion = Persion()
    persion.helloworld()
    
# >>> 执行结果如下:
# >>> hello world

# >>> 我们可以看到 helloworld() 是基于 私有函数 __hello() 来执行的;
# >>> 所以我们是通过对外的函数 helloworld() 调用了内部私有函数 __hello   ;  这就是 Python 中的 封装的概念。

🍊 面向对象编程小练习

需求:

用类和对象实现银行账户的资金交易管理,包括存款、取款和打印交易详情,交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余额。

脚本示例如下:

代码语言:python
代码运行次数:0
复制
#coding: utf-8
import time

class MoneyExchange(object):
    money = 0
    abstract = []
    single_bill_list = []
    bill_list =[]
    transcation_num = 0
    currency_type = "人民币"
    service_option_num = []
    service_option = []
    service_menu ={
        1: "1:存款",
        2: "2:取款",
        3: "3:查看明细",
        4: "4:查看余额",
        0: "0:退出系统"
    }
    for key, value in service_menu.items():
        service_option_num.append(key)
        service_option.append(value)

    def welcome_menu(self):
        print('*' * 20 + '欢迎使用资金交易管理系统' + '*' * 20)
        for i in range(0,len(self.service_option)):
            print(self.service_option[i])
        print('*' * 60)

    def save_money(self):
        self.money_to_be_save = float(input('请输入存钱金额:'))
        self.abstract = '转入'
        self.time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        self.money += self.money_to_be_save
        self.single_bill_list.append(self.time)
        self.single_bill_list.append(self.abstract)
        self.single_bill_list.append(self.money_to_be_save)
        self.single_bill_list.append(self.currency_type)
        self.single_bill_list.append(self.money)
        self.bill_list.append(self.single_bill_list)
        self.single_bill_list = []
        self.transcation_num += 1
        print('已成功存入!当前余额为:%s 元' % self.money)
        input('请点击任意键以继续...')

    def withdraw_money(self):
        self.money_to_be_withdraw = float(input('请输入取出金额:'))
        if self.money_to_be_withdraw <= self.money:
            self.abstract = '取出'
            self.time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            self.money -= self.money_to_be_withdraw
            self.single_bill_list.append(self.time)
            self.single_bill_list.append(self.abstract)
            self.single_bill_list.append(self.money_to_be_withdraw)
            self.single_bill_list.append(self.currency_type)
            self.single_bill_list.append(self.money)
            self.bill_list.append(self.single_bill_list)
            self.single_bill_list = []
            self.transcation_num += 1
            print('已成功取出!当前余额为:%s 元' % self.money)
            input('请点击任意键以继续...')
        else:
            print('您输入的取出金额超过余额,无法操作!请重新输入')
            input('请点击任意键以继续...')

    def check_bill_list(self):
        print('|      交易日期      |  摘要  |  金额  |  币种  |  余额  |')
        for i in range(0, self.transcation_num):
            print("|%s | %s | %s | %s | %s|" % (
                self.bill_list[i][0],
                self.bill_list[i][1],
                self.bill_list[i][2],
                self.bill_list[i][3],
                self.bill_list[i][4]
            ))
        input('请点击任意键以继续...')

    def check_money(self):
        print('账户余额为:%s元' % self.money)
        input('请点击任意键以继续...')

    def user_input(self):
        option = float(input('请输入选项:'))
        if option in self.service_option_num:
            if option == 1:
                self.save_money()
            if option == 2:
                self.withdraw_money()
            if option == 3:
                self.check_bill_list()
            if option == 4:
                self.check_money()
            if option == 0:
                print('您已成功退出,谢谢!')
                exit()
        else:
            print('抱歉,你输入有误,请重新输入!')
            input('请点击任意键以继续...')

            
money_exchange = MoneyExchange()
while True:
    money_exchange.welcome_menu()
    money_exchange.user_input()
    
# >>> 执行结果如下:
# >>> ********************欢迎使用资金交易管理系统********************
# >>> 1:申请存款
# >>> 2:申请取款
# >>> 3:查看明细
# >>> 4:查看余额
# >>> 0:退出系统
# >>> ************************************************************

# >>> 根据提示执行对应的操作

PS:这个脚本并不完善,但是太晚了不想改了。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🍊 什么是私有函数和私有变量
  • 🍊 私有函数与私有变量的定义方法
  • 🍊 Python 中的封装
  • 🍊 面向对象编程小练习
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档