>>>classMyClass:defPublicMethod(self):print'publicmethod'def__PrivateMethod(self):print'thisisprivate!'>>>obj=MyClass()>>>obj.PublicMethod()publicmethod>>>obj.__PrivateMethod()Traceback(mostrecentcalllast):File"",line1,inAttributeError:MyClassinstancehasnoattribute'__PrivateMethod'>>>dir(obj)['_MyClass__PrivateMethod','__doc__','__module__','PublicMethod']>>>obj._MyClass__PrivateMethod()thisisprivate!如上的执行,为什么到了obj.__PrivateMethod()就会出错,为何会这样?
添加回答
举报
0/150
提交
取消