Python------继承
class people: def __init__(self,name,gender,address,country): self.__name=name self.__gender=gender self.__address=address self.__country=country self.__info=[self.__name,self.__gender,self.__address,self.__country] def getName(self): return self.__name def getGender(self): return self.__gender def getAddress(self): return self.__address def getCountry(self): return self.__country class people2(people): def __init__(self,name,address,country="马来西亚"): #“马来西亚”为设置的默认值 self.__name=name self.__address=address self.__gender="女" self.__country=country people.__init__(self,self.__name,self.__gender,self.__address,self.__country) pe=people("苏苏","男","陕西省","China") print pe.getName() print "******************" pe2=people2("小周","山西省","越南") print pe2.getName() print pe2.getGender() print pe2.getAddress() print pe2.getCountry() print "/*/*/*/*/*/*/*/*/*" pe3=people2("小海","海南") print pe3.getName() print pe3.getAddress() print pe3.getCountry()
【无法继承私有成员!】
结果:
苏苏
******************
小周
女
山西省
越南
/*/*/*/*/*/*/*/*/*
小海
海南
马来西亚