封装

    封装数据

            多 --> 一

面向过程

"""
list_commodity_infos = [ 
    {"cid": 1001, "name": "屠龙刀", "price": 10000},
    {"cid": 1002, "name": "倚天剑", "price": 10000},
    {"cid": 1003, "name": "金箍棒", "price": 52100},
    {"cid": 1004, "name": "口罩", "price": 20},
    {"cid": 1005, "name": "酒精", "price": 30},
]
# 1.  定义函数,打印所有商品信息,格式:商品编号xx,商品名称xx,商品单价xx.
def print_commodity_infos():
    for commodity in list_commodity_infos:
        print(f"编号:{commodity['cid']},商品名称:{commodity['name']},商品单价:{commodity['price']}")
"""

面向对象

class Commodity:
    def __init__(self, cid=0, name="", price=0):
        self.cid = cid
        self.name = name
        self.price = price
list_commodity_infos = [
    Commodity(1001, "屠龙刀", 10000),
    Commodity(1002, "倚天剑", 10000),
    Commodity(1003, "金箍棒", 52100),
    Commodity(1004, "口罩", 20),
    Commodity(1005, "酒精", 30),
]
def print_commodity_infos():
    for commodity in list_commodity_infos:
        print(f"编号:{commodity.name},商品名称:{commodity.name},商品单价:{commodity.price}")

print_commodity_infos()

    封装行为 - 隐藏

          本质:障眼法

               看似是双下划线开头的变量   __data
               实际是单下划线开头+类名+双下划线开头的变量  _MyClass__data

          注意:类外不能访问,类内可以正常使用.

          私有成员:

              在类中,使用双下划线命名的成员

          适用性:

              不希望类外访问的成员
class MyClass:
    def __init__(self):
        self.__data = 10
    def __func01(self):
        print("func01执行了")
        print("私有变量是:", self.__data)

m01 = MyClass()
# print(m01.__dict__)
# print(m01._MyClass__data)
# dir函数可以获取该变量存储的数据所有成员
print(dir(m01))
# m01.__func01()
m01._MyClass__func01()


   引入 属性@property

    保护数据有效性
class Wife:
    def __init__(self, name="", age=0):
        self.name = name
        self.age = age
    @property
    def age(self):
        return self.__age
    @age.setter
    def age(self, value):
        if 23 <= value <= 30:
            self.__age = value
        else:
            raise Exception("我不要")

w01 = Wife("双儿", 25)
print(w01.age)
print(w01.__dict__)

    属性原理

          属性 = 读取函数 + 写入函数
class Wife:
    def __init__(self, age=0):
        # 因为先有age的类变量,所以本行代码访问类变量
        self.age = age
    def get_age(self):
        return self.__age
    def set_age(self, value):
        # 因为被保护
        # 所以实际存储数据的是私有变量__age
        self.__age = value
    # 创建属性对象
    # 绑定读取函数
    # 创建与实例变量名称相同的类变量(类变量先执行)
    age = property(get_age)
    # 调用属性的setter函数
    # 绑定写入函数
    # 将返回值(属性对象)赋值给类变量
    age = age.setter(set_age)

w01 = Wife(25)
print(w01.age)

    属性 各种写法

        保护实例变量
            在读写过程中,增加逻辑判断
            只能读取
            只能写入

写法1: 读写属性
# 快捷键:props + 回车
"""
class MyClass:
    def __init__(self, data):
        self.data = data # 执行写入函数
    @property
    def data(self):
        return self.__data
    @data.setter
    def data(self, value):
        self.__data = value
m01 = MyClass(10)
print(m01.data)
"""
写法2: 只读属性
# 快捷键:prop + 回车
# 为私有变量,提供读取功能
"""
class MyClass:
    def __init__(self, data):
        self.__data = data
    @property
    def data(self):
        return self.__data

m01 = MyClass(10)
print(m01.data)
"""
写法3: 只写属性
# 快捷键:无
class MyClass:
    def __init__(self, data):
        self.data = data
    data = property()
    @data.setter
    def data(self, value):
        self.__data = value
    # def set_data(self, value):
    #     self.__data = value
    #
    # data = property(None,set_data)
m01 = MyClass(10)
print(m01.__dict__)
Live what we love, do what we do, follow the heart, and do not hesitate.
原文地址:https://www.cnblogs.com/failan/p/13661266.html