python零碎知识(5)--对象

来源:互联网 发布:阿里云客服热线 编辑:程序博客网 时间:2024/05/16 12:20

1.多态:
对不同类的对象使用相同的操作,意味着就算不知道变量所引用的对象类型是什么,还能对它进行操作,而它也会根据对象(类)类型的不同表现出不同的行为。
2.方法:绑定到对象身上的函数
3.封装:程序中的其他部分隐藏对象的具体实现细节的原则。
4.使方法或者特性变成私有(从外部无法访问),对象内部可以进行访问
实现:在名字前面加上双下划线:

class Secretive(object):    def __inaccessible(self):        print('It\'s inaccessible')    def accessible(self):        print('The message is')        self.__inaccessible()>>>s=Secretive()>>>s.__inaccessible() AttributeError: 'Secretive' object has no attribute '__inaccessible'>>>s.accessible()The message isIt's inaccessible

在外部访问私有函数的方法:在似有函数名前加单下划线和类名:example._class__privatefunciton

>>>s._Secretive__inaccessible()It's inaccessible