面向对象化(类属性)

类属性(二)

私有属性__*在类里面, 双下划线开头的变量名称为私有属性;
class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score
a=Student()
print a.__score()
---->>结果报错,
Traceback (most recent call last):
  File "/Python/root/day08.py", line 12, in <module>
    print a.__score()
AttributeError: 'Student' object has no attribute '__score'
##Student对象没有__score属性
Process finished with exit code 1 ##退出码为1(非正常退出)
怎么调用Book类的私有属性__score呢?定义一个state方法
class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score

    def get_score(self):    ##定义该私有属性方法state
        return self.__score

a=Student()
print a.get_score()
---->>
100

对于父类的私有属性和方法,子类可以继承,如下

class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score

    def get_score(self):
        return self.__score

class MathStudent(Student):
    pass

a=MathStudent()
print a.name
print a.get_score()
---->>结果
xiaoming
100
@property: 定义一个类方法为私有属性的名称;让用户可以直接访问, 但不能任意修改;
class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score
    @property
    def score(self):
        return self.__score

class MathStudent(Student):
    pass

a=MathStudent()
print a.name
print a.score
a.score=98
print a.score
---->>结果
xiaoming
100
Traceback (most recent call last):
  File "/Python/root/day08.py", line 17, in <module>
    a.score=98
AttributeError: can't set attribute  ##不能设置(变动)属性

Process finished with exit code 1
@属性名.setter: 给属性赋值时先做判断; 当属性名=value会自动调用该函数;
class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score
    @property
    def score(self):
        return self.__score
   # @属性名.seter: 给属性赋值时先做判断; 当属性名=value会自动调用该函数;
    @score.setter
    def score(self,value):
        if value in range(101):
            self.__score =value
        else:
            raise TypeError("成绩0到100")
class MathStudent(Student):
    pass

a=MathStudent()
print a.name
print a.score,'\n'
a.score=98
print a.score
---->>结果:
xiaoming
100 

98
属性名.deleter,当del 属性名, 会自动调用该函数;
class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score
    @property
    def score(self):
        return self.__score

    @score.setter
    def score(self,value):
        if value in range(101):
            self.__score =value
        else:
            raise TypeError("成绩0到100")
    @score.deleter
    def score(self):
        print '调用该函数,删除中...'
class MathStudent(Student):
    pass

a=MathStudent()
print a.name
print a.score,'\n'
a.score=98
print a.score
del a.score
---->>结果
xiaoming
100 

98
调用该函数,删除中...
练习:
class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score
    @property
    def score(self):
        return self.__score

    @score.setter
    def score(self,value):
        if value in range(101):
            self.__score =value
        else:
            raise TypeError("成绩0到100")
    @score.deleter
    def score(self):
        print '调用该函数,删除中...'
class MathStudent(Student):
    def __init__(self,name,score,ID):
        super(MathStudent, self).__init__(name,score)
        self.__ID=ID
    @property
    def ID(self):
        return self.__ID
    @ID.setter
    def ID(self,value):
        return self.__ID

a=MathStudent('老李',80,'131104053')
print a.name,a.ID,a.score
a.__ID=110
print a.__ID
---->>结果:
老李 131104053 80
110
事实上,私有属性实质是把self._score改名为self.类名__score;
class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score

a=Student()
print a.name
print a._Student__score
-->>结果
xiaoming
100

小结

私有属性在该类的其他方法中可以使用;
外部不能直接调用私有属性;
私有属性实质是把self._score改名为self.类名__score;
在类里面, 双下划线开头的函数名称为私有方法;父类的私有方法, 子类不能继承;如下示例
class Student(object):
    def __init__(self,name='xiaoming',score=100):
        self.name=name
        self.__score = score

    def __get_score(self):
        return self.__score

a=Student()
print a.name
print a._Student__get_score()
--->>结果:这个当然没毛病可以调用,a是Student的实例
xiaoming
100
再看这个,子类对于父类非私有方法的继承,当然时可以的
class Student(object):
    def __init__(self,name='xiaoming',score=300):
        self.name=name
        self.__score = score

    def __get_score(self):
        return self.__score
    def doc(self):
        return '这不是私有方法'
class MathStudnet(Student):
    def __init__(self,name,score,IQ):
        super(MathStudnet, self).__init__(name,score)
        self.IQ=IQ
    def pri(self):
        super(MathStudnet, self).__get_score()
        return '这是测试能否继承父类的私有方法'
    def pub(self):
        super(MathStudnet, self).doc()
        return '这是测试能否继承父类的非私有方法'
a=MathStudnet('xiaoli',50,130)
print a.name
print a.pub()
--->>结果:
xiaoli
这是测试能否继承父类的非私有方法
再测试下对父类私有方法的继承情况,失败
class Student(object):
    def __init__(self,name='xiaoming',score=300):
        self.name=name
        self.__score = score

    def __get_score(self):
        return self.__score
    def doc(self):
        return '这不是私有方法'
class MathStudnet(Student):
    def __init__(self,name,score,IQ):
        super(MathStudnet, self).__init__(name,score)
        self.IQ=IQ
    def pri(self):
        super(MathStudnet, self).__get_score()
        return '这是测试能否继承父类的私有方法'
    def pub(self):
        super(MathStudnet, self).doc()
        return '这是测试能否继承父类的非私有方法'
a=MathStudnet('xiaoli',50,130)
print a.name
print a.pri()
--->>结果:
xiaoli
Traceback (most recent call last):
  File "/Python/root/day08.py", line 57, in <module>
    print a.pri()
  File "/Python/root/day08.py", line 49, in pri
    super(MathStudnet, self).__get_score()
AttributeError: 'super' object has no attribute '_MathStudnet__get_score'
####报错找不到该属性,子类MathStudent的实例a无法继承父类Student的私有属性,退出码当然不是0(正常退出)了
Process finished with exit code 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值