Python Basic - 面向对象高级--成员修饰符(__)

成员修饰符(__)

在定义类时,如果在字段或者方法名前在两个下划线就变成类私有的了,类私有的有字段跟方法,这些私有字段与方法在外部无法通过对象直接进行访问,但是在类的内部可以访问,所以可以在内部使用一个方法来访问这个私有方法或者字段,然后外部再调用这个方法。使用这种间接调用的方式进行调用。

私有普通字段:

直接访问私有字段示例

class Foo:
    def __init__(self,name,age):
        self.name = name
        self.__age = age#age前有两个下划线,所以这个字段就是私有的字段,在外部是无法直接访问的


obj = Foo("Elon Musk",34)
print(obj.name)
print(obj.__age)

"""
Elon Musk
Traceback (most recent call last):
  File "E:/Python/Pycharm/Lab/week10/modifiler.py", line 16, in <module>
    print(obj.__age)
AttributeError: 'Foo' object has no attribute '__age'
"""

age前有两个下划线,所以这个字段就是私有的字段,在外部是无法直接访问的

间接访问私有字段示例:

class Foo:
    def __init__(self,name,age):
        self.name = name
        self.__age = age#age前有两个下划线,所以这个字段就是私有的字段,在外部是无法直接访问的

    def show(self): # 通过创建另外一个方法,这个方法在类的内部,所以是可以访问私有字段的,这个方法又可以在外部调用,所以可以返回。
        return self.__age


obj = Foo("Elon Musk",34)
print(obj.name)
#print(obj.__age)
print(obj.show())

"""
Elon Musk
34
"""

过创建另外一个方法,这个方法在类的内部,所以是可以访问私有字段的,这个方法又可以在外部调用,所以可以返回。

私有静态字段

直接访问私有静态字段示例:

class Foo:
    __field = "Elon Musk"

    def __init__(self):
        pass

print(foo.__field)

"""
Traceback (most recent call last):
  File "E:/Python/Pycharm/Lab/week10/modifiler.py", line 7, in <module>
    print(foo.__field)
NameError: name 'foo' is not defined
"""

间接访问私有静态字段示例:

class Foo:
    __field = "Elon Musk"

    def __init__(self):
        pass

    def show(self):# 通过在类内部定制一个方法,通过这个方法来访问静态字段,外部通过访问这个方法来实现间接访问静态字段
        return self.__field

print(Foo().show())

"""
Elon Musk
"""

通过在类内部定制一个方法,通过这个方法来访问静态字段,外部通过访问这个方法来实现间接访问静态字段

静态方法实现:

在这里插入图片描述

私有普通方法

直接访问私有普通方法示例:

class Foo:
    def __func(self):
        return "Tesla"

obj = Foo()
print(obj.__func)

"""
Traceback (most recent call last):
  File "E:/Python/Pycharm/Lab/week10/modifiler.py", line 6, in <module>
    print(obj.__func)
AttributeError: 'Foo' object has no attribute '__func'
"""

间接访问私有普通方法:

class Foo:
    def __func(self):
        return "Tesla"

    def show(self):
        result = self.__func()
        return result

obj = Foo()
print(obj.show())


"""
Tesla
"""

私有静态方法

直接访问私有静态方法

class Foo:
    @staticmethod
    def __func():
        print("Tesla")

Foo.__func()
"""
Traceback (most recent call last):
  File "E:/Python/Pycharm/Lab/week10/modifiler.py", line 6, in <module>
    Foo.__func()
AttributeError: type object 'Foo' has no attribute '__func'

"""

间接访问私有静态方法

class Foo:
    @staticmethod
    def __func():
        print("Tesla")

    def show(self):
        result = Foo.__func()
        return result

obj = Foo()
obj.show()


"""
Tesla
"""

属性与方法一样

属性往往不会搞成私有的,搞成私有的通常都是方法或者字段
字段搞成这个,比如人连接数据库,但是数据库的用户名密码不应该对外暴露,应该放在类里面,避免密码泄漏

有继承的情况

子类是否可以访问父类的私有方法

class Father:
    def __Fatherfunc(self):
        print("Fatherfunc")

class son(Father):
    def son(self):
        print("Son")

obj = son()
obj.son()
obj.__Fatherfunc()
"""
Son
Traceback (most recent call last):
  File "E:/Nextcloud/NAS/Study/Study-Note/Python/Pycharm/Lab/week10/modifiler.py", line 11, in <module>
    obj.__Fatherfunc()
AttributeError: 'son' object has no attribute '__Fatherfunc'
"""

== 无法直接访问父类的私有方法==

class Father:
    def __Fatherfunc(self):
        print("Fatherfunc")

class son(Father):
    def son(self):
        print("Son")
    def show(self):
        super(son,self).__Fatherfunc()
obj = son()
obj.son()
obj.show()
#obj.__Fatherfunc()
"""
Son
Traceback (most recent call last):
  File "E:/Python/Pycharm/Lab/week10/modifiler.py", line 12, in <module>
    obj.show()
  File "E:/Python/Pycharm/Lab/week10/modifiler.py", line 9, in show
    super(son,self).__Fatherfunc()
AttributeError: 'super' object has no attribute '_son__Fatherfunc'
"""

== 继承的情况下,间接也无法访问父类的私有方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值