python3 私有化 属性property

私有化

  xx:公有变量

  • _x:单前置下划线,私有化属性或方法,from somemodule import *禁止导入,类对象和子类可以访问
  • __xx:双前置下划线,避免与子类中的属性命名冲突,无法在外部直接访问(名字重整所以访问不到)
  • __xx__:双前后下划线,用户名字空间的魔法对象或属性。例如__init__,不要自己发明这样的名字。
  • xx_:单后置下划线,用于避免与Python关键值字冲突。
#! /usr/bin/env python3

class Person(object):
    def __init__(self, name, age, taste):
        self.name = name
        self._age = age
        self.__taste = taste


    def showperson(self):
        print(self.name)
        print(self._age)
        print(self.__taste)

    def dowork(self):
        self._work()
        self.__away()

    def _work(self):
        print('my_work')

    def __away(self):
        print('my__away')


class Student(Person):
    def construction(self, name, age, taste):
        self.name = name
        self._age = age
        self.__taste = taste


    def showstudent(self):
        print(self.name)
        print(self._age)
        print(self.__taste)

    @staticmethod
    def testbug():
        _Bug.showbug()

#模块内可以访问,当from  cur_module import *时,不导入

class _Bug(object):
    @staticmethod
    def showbug():
        print('showbug')

s1 = Student('jack', 25, 'football')
s1.showperson()
print('*'*20)


#无法访问__taste,导致报错
#s1.showstudent()

s1.construction('rose', 30, 'basketball')
s1.showperson()
print('*'*20)

s1.showstudent()
print('*'*20)

Student.testbug()

 

  总结:

  1.   父类中属性名为__名字 的,子类不继承,子类不能访问
  2.   如果在子类中向 __名字 赋值,那么会在子类中定义的一个与父类相同名字的属性
  3.   _名 的变量、函数、类在使用from xxx import *时不会被导入。

属性 property

私有属性添加getter和setter方法

 

#! /usr/bin/env python3

class Money(object):
    def __init__(self):
        self.__money = 0

    def getMoney(self):
        return self.__money

    def setMoney(self, value):
        if isinstance(value, int):
            self.__money = value
        else:
            print('error:不是整型数字')

 

使用property升级getter和setter方法

#! /usr/bin/env python3

class Money(object):
    def __init__(self):
        self.__money = 0

    def getMoney(self):
        return self.__money

    def setMoney(self, value):
        if isinstance(value, int):
            self.__money = value
        else:
            print('error:不是整型数字')

    money = property(getMoney, setMoney)

 

 

使用property 取代getter和setter方法

@property 成为属性函数,可以对属性赋值时作必要的检查,并保证代码的清晰短小,主要有两个作用

  • 将方法转换为只读
  • 重新实现一个属性的设置和读取方法,可做边界判定

 

#! /usr/bin/env python3

class Money(object):
    def __init__(self):
        self.__money = 0

    @property
    def money(self):
        return self.__money

    @money.setter
    def money(self, value):
        if isinstance(value, int):
            self.__money = value
        else:
            print('error:不是整型数字')

 

 

posted @ 2018-03-19 23:14  Guan_zi  阅读(398)  评论(1编辑  收藏  举报