Python面向对象:在类中定义私有变量和私有方法,python面向对象,# Demonstrat


# Demonstrates private variables and methodsclass Critter(object):    '''A virtual pet'''    def __init__(self, name, mood):        print 'A new critter has been born!'        self.name = name            # public attribute        self.__mood = mood          # private attribute    def talk(self):        print '\nI'm', self.name        print 'Right now I feel', self.__mood, '\n'    def __private_method(self):        print 'This is a private method.'    def public_method(self):        print 'This is a public method.'        self.__private_method()# maincrit = Critter(name = 'Poochie', mood = 'happy')crit.talk()crit.public_method()

评论关闭