zoukankan      html  css  js  c++  java
  • python之路--面向对象之封装

    封装

    什么是封装?

    隐藏对象的属性和实现细节,仅对外提供公共访问方式。

    好处

    1. 将变化隔离
    2. 便于使用
    3. 提高复用性
    4. 提高安全性

    封装原则

    1. 将不需要对外提供的内容都隐藏起来;
    2. 把属性都隐藏,提供公共方法对其访问。

    封装方法

    在python中用下划线开头的方式,将属性隐藏起来(设置成私有)即封装

    私有属性

    私有属性一

    class Person:
        def __init__(self,height,weight,name,sex):
            self.__height = height            # 私有属性:外部不能调用
            self.weight = weight
            self.name = name
            self.sex = sex
    
        def tell_bmi(self):
            return self.weight / self.__height ** 2
        def tell_height(self):      # 建议这样查看值,用户不可以直接修改
            return self.__height
    
        def set_height(self,new_height):        # 建议这样修改值,即安全又人性化
            self.__height = new_height if new_height > 20 else self.__height
            return self.__height            # return不能返回赋值语句
    
    
    dennis = Person(1.75,120,'dennis','man')
    print(dennis.tell_bmi())
    print(dennis.__dict__)
    print(dennis._Person__height)
    print(dennis.tell_height())
    print(dennis.set_height(-123))      # 设置不成功
    
    # 私有属性:
    # 在本类中就可以正常使用
    # 在本类外就必须_类名__属性名调用(不建议使用)

    私有属性二

    class Goods:
        __price = 3         ### 私有公共属性
        def __init__(self,name,num):
            self.name = name
            self.num = num
        def goods_price(self):
            return self.num * Goods.__price
    
    banana = Goods('egon',2)
    print(banana.goods_price())
    print(Goods.__dict__)
    print(Goods._Goods__price)
    

    私有方法

    class Foo:
        def __init__(self,height,weight):
            self.height = height
            self.weight = weight
    
        def tell_bmi(self):
            return self.weight / self.__height_pow()
        def __height_pow(self): # 私有方法
            return self.height ** 2
    
    
    dennis = Foo(1.7,125)
    print(dennis.tell_bmi())
    print(dennis._Foo__height_pow())    # 不建议

    property属性

    什么是property

    property 把类中一个方法变成了一个方法去实现
    @property 实现了只读
    @property x.setter 实现了可读可写(x.setter依赖@property,x为被装饰方法的方法名)

    class Goods:
        __discount = 0.8    # 类的私有属性
        def __init__(self,name,price):
            self.name = name
            self.__price = price
    
        @property        # 只读
        def price(self):
            return self.__price * Goods.__discount
    
        @price.setter      # 可写
        def price(self,new_price):
            self.__price = new_price
    
        @price.deleter
        def price(self):
            del self.__price    # 只能删除self赋值的命名
    #       del self.name
            print('在执行删除操作啦!')
    apple = Goods('apple',20)
    print(apple.price)
    apple.price = 40
    print(apple.price)
    
    del apple.price     # 删除会执行price.deleter里的
    # print(apple.price)          # 因为删掉了__price,所以再次print会报错
    

    用namedtuple实现只有属性的类

    from collections import namedtuple
    
    Course = namedtuple('Course',['price','period','name'])
    python = Course(20000,'6 months','python')
    print(python.name)
    





  • 相关阅读:
    LCT 动态树 模板
    [HNOI2010] 物品调度 fsk
    [HNOI2010] 矩阵 matrix
    [HNOI2010] 平面图判定 planar
    [HNOI2010] 公交线路 bus
    [HNOI2017]抛硬币
    [HNOI2010] 弹飞绵羊 bounce
    [HNOI2010] 合唱队 chorus
    [HNOI2017]礼物
    [HNOI2017]大佬
  • 原文地址:https://www.cnblogs.com/8192bit/p/7366713.html
Copyright © 2011-2022 走看看