esc0rp 发表于 2024-8-17 21:17:37

三十4、Python面向对象:"假"封装


    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/d64c3ec5-ce61-4a77-bc8d-56358fe2c7e7~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895751&amp;x-signature=YZYQInrush%2FuoywZ8bsy96s%2BxNA%3D" style="width: 50%; margin-bottom: 20px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python面向对象三大特征之封装</p>
    </div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.什么是封装?</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">众所周知,封装(Encapsulation)、继承(Inherit)和多态(Polymorphism)是面向对象的三大特征,它指的是将对象的状态信息<span style="color: black;">隐匿</span>在类定义的内部,不<span style="color: black;">准许</span>外界直接<span style="color: black;">拜访</span>对象的内部信息,而是<span style="color: black;">经过</span>该类对<span style="color: black;">外边</span>暴露的<span style="color: black;">办法</span>来实现对类内部信息的<span style="color: black;">拜访</span>和操作。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2.封装的好处</h1>对外<span style="color: black;">隐匿</span>类内部的实现细节;<span style="color: black;">运用</span>者仅通<span style="color: black;">经过</span>类定义时对外暴露的<span style="color: black;">办法</span>来<span style="color: black;">拜访</span>数据,<span style="color: black;">因此呢</span>,<span style="color: black;">能够</span>在定义<span style="color: black;">办法</span>时添加<span style="color: black;">掌控</span><span style="color: black;">规律</span>,限制对属性的不<span style="color: black;">恰当</span><span style="color: black;">拜访</span>;在用户<span style="color: black;">经过</span><span style="color: black;">办法</span>设置或修改类的属性时,<span style="color: black;">能够</span>在<span style="color: black;">办法</span>上添加数据校验规则,<span style="color: black;">能够</span><span style="color: black;">保准</span>数据的完整性和有效性;<h1 style="color: black; text-align: left; margin-bottom: 10px;">3.<span style="color: black;">怎么样</span>实现封装?</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现封装<span style="color: black;">能够</span>从以下两方面来<span style="color: black;">思虑</span>:</p><span style="color: black;">必须</span>将对象的属性和实现细节<span style="color: black;">隐匿</span>起来,不<span style="color: black;">准许</span><span style="color: black;">外边</span>直接<span style="color: black;">拜访</span>和操作;将<span style="color: black;">办法</span>暴露出来,让<span style="color: black;">办法</span>来<span style="color: black;">掌控</span>这些属性的<span style="color: black;">拜访</span>与操作;<h1 style="color: black; text-align: left; margin-bottom: 10px;">4.Python类中实现封装</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>Python是解释型的语言,并<span style="color: black;">无</span>像其它语言<span style="color: black;">供给</span>变量的修饰符,<span style="color: black;">例如</span>private, protected等,而是<span style="color: black;">经过</span>一种技巧,即Python内部的约定,给类成员命名以双下划线的开头,Python就会将它们<span style="color: black;">隐匿</span>起来。</p><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Dog</span>:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">get_name</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;">return</span> <span style="color: black;">self</span>.__name

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">set_name</span><span style="color: black;">(<span style="color: black;">self</span>, name)</span></span>:
    <span style="color: black;">if</span>(len(name)&lt;<span style="color: black;">2</span>):
    raise ValueError(<span style="color: black;">狗的名字<span style="color: black;">最少</span><span style="color: black;">必须</span>两个汉字</span>)<span style="color: black;">self</span>.__name = name
    name = property(get_name, set_name)

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__pee</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    print(<span style="color: black;">我要尿尿了...</span>)

    <span style="color: black;"># 实例化并调用</span>
    d = Dog()
    <span style="color: black;"># 尝试直接给变量赋值</span>
    d.name = <span style="color: black;">泰迪</span>
    print(d.name)

    <span style="color: black;">#d.name =狗</span>
    <span style="color: black;">#print(d.name) # ValueError: 狗的名字<span style="color: black;">最少</span><span style="color: black;">必须</span>两个汉字</span>

    <span style="color: black;">#d.__pee() # AttributeError: Dog object has no attribute __pee</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上面的例子中将Dog类的实例变量命名为__name, 这个实例变量就会被<span style="color: black;">隐匿</span>起来,<span style="color: black;">这般</span>,程序<span style="color: black;">没法</span>直接<span style="color: black;">拜访</span>该实例变量,只能<span style="color: black;">经过</span>get_name, set_name这些<span style="color: black;">办法</span>来进行<span style="color: black;">拜访</span>, 而get_name, set_name会添加<span style="color: black;">掌控</span><span style="color: black;">规律</span>,<span style="color: black;">仅有</span>符合<span style="color: black;">要求</span>的name才<span style="color: black;">准许</span>被设置。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一</span>,<span style="color: black;">针对</span><span style="color: black;">隐匿</span>的<span style="color: black;">办法</span>__pee(), 程序尝试执行,<span style="color: black;">因为</span>该<span style="color: black;">办法</span>在类的内部定义为<span style="color: black;">隐匿</span>,<span style="color: black;">因此</span>直接<span style="color: black;">拜访</span>会报错。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">5.<span style="color: black;">为何</span>说Python中的封装是“假”封装?</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实,Python其实<span style="color: black;">无</span>真正的<span style="color: black;">隐匿</span>机制,双下划线实现<span style="color: black;">隐匿</span>在Python中只是一种技巧, <span style="color: black;">由于</span><span style="color: black;">针对</span>加了双下划线的变量和<span style="color: black;">办法</span>名,Python会巧妙地改名这个名字,即会在你定义的双下划线变量或<span style="color: black;">办法</span>前再添加单下划线和类名。<span style="color: black;">能够</span><span style="color: black;">经过</span>"<strong style="color: blue;">对象名.__dict__</strong>"查看,或<span style="color: black;">经过</span>"<strong style="color: blue;">对象名._类名__[<span style="color: black;">办法</span>名|变量名]</strong>"调用:</p><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Dog</span>:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">get_name</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;">return</span> <span style="color: black;">self</span>.__name

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">set_name</span><span style="color: black;">(<span style="color: black;">self</span>, name)</span></span>:
    <span style="color: black;">if</span>(len(name)&lt;<span style="color: black;">2</span>):
    raise ValueError(<span style="color: black;">狗的名字<span style="color: black;">最少</span><span style="color: black;">必须</span>两个汉字</span>)
    <span style="color: black;">self</span>.__name = name
    name = property(get_name, set_name)<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__pee</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    print(<span style="color: black;">我要尿尿了...</span>)

    <span style="color: black;"># 实例化并调用</span>
    d = Dog()
    d.name =<span style="color: black;">泰迪</span>

    <span style="color: black;"># <span style="color: black;">经过</span>下面的<span style="color: black;">办法</span><span style="color: black;">能够</span>查看被<span style="color: black;">隐匿</span>的成员变量和<span style="color: black;">办法</span></span>print(d._Dog__name)
    d._Dog__name=<span style="color: black;">藏獒</span>
    print(d._Dog__name)
    d._Dog__pee()<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/394eb2f74b8347bfa7317a6612f730df~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895751&amp;x-signature=P4RJAu3XbBeDNCynqt4zZzFzlM8%3D" style="width: 50%; margin-bottom: 20px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">查看<span style="color: black;">隐匿</span>变量和<span style="color: black;">办法</span></p>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>,从上面<span style="color: black;">能够</span>看出,Python并<span style="color: black;">无</span>真正地实现“<span style="color: black;">隐匿</span>”机制,是一种“<strong style="color: blue;">假</strong>”封装,<span style="color: black;">然则</span><span style="color: black;">咱们</span>不<span style="color: black;">举荐</span><span style="color: black;">这般</span><span style="color: black;">拜访</span>,<span style="color: black;">一般</span><span style="color: black;">亦</span>不会这么<span style="color: black;">拜访</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>,<span style="color: black;">倘若</span><span style="color: black;">必须</span><span style="color: black;">隐匿</span>变量或成员<span style="color: black;">办法</span>,<span style="color: black;">能够</span><span style="color: black;">运用</span>以“双下划线”命名的这种方式,Python<span style="color: black;">亦</span><span style="color: black;">举荐</span>这种<span style="color: black;">运用</span>方式。</p>




听听海 发表于 2024-9-9 00:03:19

我赞同你的看法,你的智慧让人佩服,谢谢分享。

7wu1wm0 发表于 2024-11-2 04:26:18

我们有着相似的经历,你的感受我深有体会。
页: [1]
查看完整版本: 三十4、Python面向对象:"假"封装