(董付国)Python 学习笔记---Python面向对象程序设计(2)

6.1.4 私有成员与公有成员

  • Python并没有对私有成员提供严格的访问保护机制。
    √ 在定义类的成员时,如果成员名以两个下划线“__”或更多下划线开头而不以两个或更多下划线结束则表示是私有成员
    √ 私有成员在类的外部不能直接访问,需要通过调用对象的公开成员方法来访问,也可以通过Python支持的特殊方式来访问。
  • 公开成员既可以在类的内部进行访问,也可以在外部程序中使用。
>>> class A:
...     def __init__(self,value1 = 0,value2 = 0):
...             self._value1 = value1
...             self.__value2 = value2
...     def setValue(self,value1,value2):
...             self._value1 = value1
...             self.__value2 = value2
...     def show(self):
...             print(self._value1)
...             print(self.__value2)
...
>>> a = A()
>>> a._value1
0
>>> a._A__value2                                #在外部访问私有数据成员的特殊方法
0
  • 在IDLE环境中,在对象或类名后面加上一个圆点“.”,稍等一秒钟则会自动列出其所有公开成员,模块也具有同样的方法。
  • 如果在圆点“.”后面再加一个下划线,则会列出该对象、类或模块的所有成员,包括私有成员。也可以通过dir()来访问所有的方法。
  • 在Python中,以下划线开头的变量名和方法名有特殊的含义。尤其是在类的定义中。用下划线作为变量名和方法名前缀和后缀来表示类的特殊成员:
    √ _ xxx:受保护成员,不能用‘from module import *’导入;
    √ __xxx __:系统定义的特殊成员
    √ __xxx:私有成员,只有类对象自己能访问,子类对象不能直接访问到这个成员,但在对象外部可以通过“对象名._类名 __xxx”这样的特殊方式来访问。

※ 注意:Python中不存在严格意义上的私有成员。

  • 在程序中,可以使用一个下划线来表示不关心该变量的值。
>>> for _ in range(5):
...     print(3,end = ' ')
...
3 3 3 3 3 >>>
>>> a, _ =divmod(60,18)                 #只关心整商(赋给a),不关心余数(赋给 _ )
>>> a
3
  • 下面的代码演示了特殊成员定义和访问的方法:
>>> class Fruit:
...     def __init__(self):
...             self.__color = 'Red'
...             self.price = 1
...
>>> apple = Fruit()
>>> apple.price                         #显示对象公开数据成员的值
1
>>> apple.price = 2                     #修改对象公开数据成员的值
>>> apple.price
2
>>> print(apple.price,apple._Fruit__color)      #显示对象私有数据成员的值
2 Red
>>> apple._Fruit__color = "Blue"        #修改对象私有数据成员的值
>>> print(apple.price,apple._Fruit__color)
2 Blue
>>> print(apple.__color)                #不能直接访问对象的私有数据成员,出错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Fruit' object has no attribute '__color'

6.2 方法

  • 在类中定义的方法可以粗略分为四大类:公有方法,私有方法,静态方法和类方法。(抽象方法)
    √ 公有方法、私有方法都属于对象,私有方法的名字以两个下划线“__”开始,每个对象都有自己的公有方法和私有方法,在这两类方法中可以访问属于类和对象的成员;
    √ 公有方法通过对象名直接调用,私有方法不能通过对象名直接调用,只能在属于对象的方法中通过self调用或在外部通过Python支持的特殊方式来调用。
    √ 如果通过类名来调用属于对象的公有方法,需要显示为该方法的self参数传递一个对象名,用来明确指定访问哪个对象的数据成员。
    √ 静态方法和类方法都可以通过类名和对象名调用,但不能直接访问属于对象的成员,只能访问属于类的成员。
    √ 静态方法和类方法都可以通过类名和对象名调用,但不能直接访问属于对象的成员。
    √ 静态方法可以没有参数。
    √ 一般将cls作为类方法的第一个参数名称,但也可以使用其他名字作为参数,并且在调用类方法时不需要为该参数传递数值。
>>> class Root:
...     __total = 0
...     def __init__(self,v):           #构造方法
...             self.__value = v
...             Root.__total +=1
...     def show(self):                 #普通实例方法
...             print('self.__value:',self.__value)
...             print('self.__total:',self.__total)
...     @classmethod                    #修饰器,声明类方法
...     def classShowTotal(cls):        #类方法
...             print(cls.__total)
...     @staticmethod                   #修饰器,声明静态方法
...     def staticShowTotal():          #静态方法
...             print(Root.__total)
...
>>> r = Root(3)
>>> r.classShowTotal()                  #通过对象来调用类方法
1
>>> r.staticShowTotal()                 #通过对象来调用静态方法
1
>>> r.show()
self.__value: 3
self.__total: 1
>>> rr = Root(5)
>>> Root.classShowTotal()               #通过类名调用类方法
2
>>> Root.staticShowTotal()              #通过类名调用静态方法
2
>>> Root.show()                         #试图通过类名直接调用实例方法,失败
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: show() missing 1 required positional argument: 'self'
>>> Root.show(r)                        #但是可以通过这种方法来调用方法并访问实例成员
self.__value: 3
self.__total: 2
>>> Root.show(rr)                       #通过类名调用实例方法时为self参数显式传递对象 名
self.__value: 5
self.__total: 2

6.3.2 Python 3.x中的属性

  • 在Python 3.x中,属性得到了较完整的实现,支持更加全面的保护机制。属性包括读,写和删除。

※只读属性

>>> class Test:
...     def __init__(self,value):
...             self.__value = value
...     @property
...     def value(self):        #只读,无法修改和删除
...             return self.__value
...
>>> t = Test(3)
>>> t.value
3
>>> t.value = 5                 #只读属性不允许修改值
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
>>> t.v = 5                     #动态增加新成员
>>> t.v
5
>>> del t.v                     #动态删除成员
>>> del t.value                 #试图删除对象属性,失败
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't delete attribute
>>> t.value
3

※可读、可写属性

>>> class Test:
...     def __init__(self,value):
...             self.__value = value
...     def __get(self):
...             return self.__value
...     def __set(self,v):
...             self.__value = v
...     value = property(__get,__set)
...     def show(self):
...             print(self.__value)
...
>>> t = Test(5)
>>> t.value                             #允许读取属性值
5
>>> t.value = 5                         #允许修改属性值
>>> t.show()                            #属性对应的私有变量也得到了相应的修改
5
>>> del t.value                         #试图删除属性,失败
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't delete attribute
>>> class Test:
...     def __init__(self,value):
...             self.__value = value
...     def __get(self):
...             return self.__value
...     def __set(self,v):
...             self.__value = v
...     def __del(self):
...             del self.__value
...     value = property(__get,__set,__del)
...     def show(self):
...             print(self.__value)
...
>>> t = Test(3)
>>> t.show()
3
>>> t.value
3
>>> t.value = 5
>>> t.show()
5
>>> t.value
5
>>> del t.value
>>> t.show()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 12, in show
AttributeError: 'Test' object has no attribute '_Test__value'
>>> t.value = 1
>>> t.show()
1

6.4.1 常用的特殊方法

  • Python类有大量的特殊方法,其中比较常见的是构造函数和析构函数,除此之外,Python还支持大量的特殊方法,运算符重载就是通过重写特殊方法实现的。

√ Python中类的构造函数是__init__(),一般用来为数据成员设置初始值或进行其他必要的初始化工作,在创建对象时被自动调用和执行。
√ Python中类的析构函数是__del__(),一般用来释放对象占用的资源,在Python删除对象和收回对象空间时被自动调用和执行。如果用户没有编写析构函数,Python将提供一个默认的析构函数进行必要的清理工作。

>>> x = [3]
>>> x += [4]
>>> x
[3, 4]
>>> x += [2]
>>> x
[3, 4, 2]
>>> dir(x)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> dir({})
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> dir(set)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
>>> dir(3)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

posted @ 2019-09-10 12:22  旅人_Eric  阅读(193)  评论(0编辑  收藏  举报