欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

面向对象封装

程序员文章站 2024-03-23 14:46:28
...
#
# _警告
# __不运行'A' object has no attribute '__money'
# 1.对数据进行破坏性访问:
class Data:
    def __init__(self,name,age):
        self.name = name
        self.age = age


d=Data('d',12)
d.age=1000
print(d.age)#1000,不符合常识
#
# #---------------------------------------------------------------------
# # 2.加一个下划线,形成约定。大家看到一个下划线,就不会访问他。但并不能保证一定能够受到保护
class Data:
    def __init__(self, name, age):
        self._name = name
        self._age = age


d = Data('d', 12)
d._age = 1000
print(d._age)#1000

# #---------------------------------------------------------------------
# # 3.加两个下划线,强制保护
class Data:
    def __init__(self,name,age):
        self.__name = name
        self.__age = age

    def set_name(self,name):
        self.__name = name

    def set_age(self,age):
            self.__age = age

    def get_name(self):
        return self.__name

    def get_age(self):
        return self.__age
d=Data('d',12)
d.set_age(109)
d.set_name('n')
print(d.get_name(),d.__age())#报错,没有__属性,达到强制保护

#---------------------------------------------------------------------
# 封装步骤:1-2-3
class Data:
    def __init__(self,name,age):
        self.__name = name#1,属性全部私有化
        self.__age = age

    def set_name(self,name):#2,设置对应方法
        self.__name = name

    def set_age(self,age):
        if age>100:#3,方法内添加访问限制条件,保护敏感类型
            print("无法修改·")
        else:
            self.__age = age

    def get_name(self):
        return self.__name

    def get_age(self):
        return self.__age
d=Data('d',12)
d.set_age(109)
d.set_name('n')
print(d.get_age(),d.get_name())

# 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# 封装后的小bug
class Data:
    def __init__(self,name,age):
        self.__name = name
        self.__age = age

    def set_name(self,name):
        self.__name = name

    def set_age(self,age):
        if age>100:
            print("无法修改·")
        else:
            self.__age = age

    def get_name(self):
        return self.__name

    def get_age(self):
        return self.__age
d=Data('d',12)
d.set_age(109)
d.set_name('n')
d._Data__age=120
print(d.get_age(),d.get_name())#120,n;避开了set方法中的条件限制,更改了数据
#-------------------------------------------------------------------------------------------------------------------------
# 单个对象拥有的扩展属性
class Data:

    def __init__(self,name,age):
        self.__name = name
        self.__age = age

    def set_name(self,name):
        self.__name = name

    def set_age(self,age):
        if age>100:
            print("无法修改·")
        else:
            self.__age = age

    def get_name(self):
        return self.__name

    def get_age(self):
        return self.__age
d=Data('d',12)
# 对象d添加nickname,h没有添加
d.nickname='k'
d.jj='l'
h=Data('d',12)
print(d.nickname, d.jj)#k,1
print(h.nickname)#'Data' object has no attribute 'nickname'
#-------------------------------------------------------------------------------------------------------------------------
# 处理扩展属性办法
class Data:
    # 处理扩展属性办法
    __slots__ = ['__name', '__age', 'nickname']

    def __init__(self,name,age):
        self.__name = name
        self.__age = age

    def set_name(self,name):
        self.__name = name

    def set_age(self,age):
        if age>100:
            print("无法修改·")
        else:
            self.__age = age

    def get_name(self):
        return self.__name

    def get_age(self):
        return self.__age
d=Data('d',12)
d.nickname='k'
d.jj='l'
h=Data('d',12)
print(h.nickname)#
print(d.nickname, d.jj)#'Data' object has no attribute 'jj'
# nickname可以添加,jj不能添加,没有定义在__slots__中