# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#http://blog.csdn.net/wklken/article/details/6313265




#类的私有属性及私方法
class MyClass():
    
    def __init__(self,name,age):

        #定义私有属性name
        self.__name=name
        self.age=age
        
    def printName(self):
        return self.__name

    #私有方法
    def __printAge(self):
        return self.age
     

if __name__=='__main__':
    print
    b=MyClass('xiaodeng',28)
    print b.printName()
    print b.age#28
    #print b.__name#弹出错误,提示为实例(instance)没有该属性(attribute),由于name为私有,所以无法访问其属性
    print b.__printAge()#错误和b.__name一致

'''
xiaodeng
28

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\te.py", line 22, in <module>
    print b.__name#
AttributeError: MyClass instance has no attribute '__name'
'''
    

相关文章:

  • 2021-08-17
  • 2021-08-17
  • 2021-08-17
  • 2021-08-12
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2021-08-17
猜你喜欢
  • 2021-08-17
  • 2022-01-18
  • 2021-12-02
  • 2021-05-23
  • 2022-12-23
  • 2022-02-03
相关资源
相似解决方案