#!/usr/bin/env python#_*_coding:utf8_*_classpeople (object): Infor='Test Info'#class VariablesInfor_dic = { "name":'Dodo', " Age": 30 } #dictionaries can be shared, shallow copy knowledge def __init__(self,name,age,job):#constructor method to implement parameter initializationSelf.name =name self.__age= Age#private variables, not externally callable, can only be called inside the class, cannot be changed externallySelf.job =Jobdef __breath(self):#Private method, cannot be called externally, can only be called inside class Print('{} is Breathing .....'. Format (self.name))defget_age (self,target):iftarget = =' Age': Print("age:{}". Format (self.__age))#calling private variables internally elifTarge = ='W.H.O.': Print('None ...') Else: Print("Hahahahhahahaha") defWalk (self):Print("{} walking ....". Format (self.name)) self.__breath()#calling private methods internally defTalk (self):Print("{} talking ....". Format (self.name))if __name__=='__main__': P1= People ("Dodo", 23,'Xiguan') Print('p1.name:{}'. Format (p1.name))#print (' p1.age:{} '. Format (P1.get_age ("Age" ))P1.get_age (' Age') P1.walk () P1.talk ()#P1.__age External cannot access private variables, if you really want to access the P1._people__ageP1._people__age = 30#It is generally not recommended to access private variables like this Print(' Age', P1._people__age)Print('name', P1.name)Print('Job', P1.job)
Python class variables