• 【Python】【技能树评测】技巧实例-说明改进和实践【04】访问限制


    前言:

    面向对象的设计方法里面,最重要的两条,一条是代码复用,通过继承的权限设置进行封装。
    Python的类封装和C++的定义类似,只是不用关键字。但是他的类型的定义没有C++丰富严格。
    CSDN的这篇视频很简单清晰的表现了这个设计思想。但是,视频不方便查阅,所以,这里重写了一下。

    Python 访问控制

    CSDN的视频里面给出的定义:
    在这里插入图片描述

    系统 【首尾双下划线】

    xx

    保护 【首为单下划线】

    【_xx】

    私有 【首为双下划线】

    【__xx】

    私有的类属性

    class Apple(): 
        def name(self,name):
           self.__name = name
           
        def getName(self):
            return self.__name
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    这里添加了一个私有的属性name,同时设定了一访问函数,getName去访问他。

    私有的函数(方法)

    class Apple(): 
        def __setAge(self,age):
           self.__age = age
           
        def getName(self):
            return self.__name
         
        def info(self,age):
        	self.__setAge(age);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    和私有的类一样,可以定义私有的方法.私有方法的调用,在类的定义内,需要给予前缀【self.】,直接调用会报错

    源码举例:

    对应技能树普遍不能运行的情况,自己编写了一个范例:
    【案,这个例子里,我写了两个私有的属性sex,和Married,一个私有函数__SetPrivateMarriedx】
    【直接调用私有属性,例如,源码中,person.__sex = “男人”,并不能改变性别的属性,仍然是女人,直到调用了我们预设的接口访问函数SetSex,在文章的最后,我们尝试调用私有函数,结果报错,说明了数据访问的权限被有效的保护了。
    person.__SetPrivateMarriedx(“Yes”)】

    class People(): 
        def __init__(self,name,work,sex,Married):
            self.name = name
            self.work = work
            self.__sex = sex
            self.__Married = Married
            
        def Spyname(self,Spyname):
           self.__name = Spyname
           
        def getName(self):
            return self.__name
        
        def SetSex(self,Sex):
            self.__sex = Sex
            self.__SetPrivateMarriedx("Yes")
            
        
        def __SetPrivateMarriedx(self,Married):
            self.__Married = Married
            
        
        def show(self):
            print("姓名",self.name)
            print("职务",self.work)
            print("性别",self.__sex)
            print("婚姻",self.__Married)
    
    
    person=People("普通人","good","女人","no")
    person.show()
    
    
    person.name = "俞敏洪"
    person.work = "新东方老板"
    person.__sex = "男人"
    person.show()
    
    
    person.SetSex("男人")
    person.show()
    
    person.__SetPrivateMarriedx("Yes")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    姓名 普通人
    职务 good
    性别 女人
    婚姻 no
    姓名 俞敏洪
    职务 新东方老板
    性别 女人
    婚姻 no
    姓名 俞敏洪
    职务 新东方老板
    性别 男人
    婚姻 Yes


    AttributeError Traceback (most recent call last)
    in
    41 person.show()
    42
    —> 43 person.__SetPrivateMarriedx(“Yes”)
    44

    AttributeError: ‘People’ object has no attribute ‘__SetPrivateMarriedx’

  • 相关阅读:
    CYEZ 模拟赛 4
    C++ 在函数中定义函数
    [vxe-table] 合并行后滚动错位
    windows下使用FFmpeg开源库进行视频编解码完整步聚
    rollup常用插件详解
    哈希的应用--位图和布隆过滤器
    Java核心篇,二十三种设计模式(十六),行为型——迭代器模式
    01 INFINI-GATEWAY简介
    被领导拒绝涨薪申请,跳槽后怒涨9.5K,这是我的心路历程
    idea插件推荐
  • 原文地址:https://blog.csdn.net/yellow_hill/article/details/125384358