Python------继承

Posted soyosuyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()

【无法继承私有成员!】

结果:

苏苏
******************
小周

山西省
越南
/*/*/*/*/*/*/*/*/*
小海
海南
马来西亚











以上是关于Python------继承的主要内容,如果未能解决你的问题,请参考以下文章

常用python日期日志获取内容循环的代码片段

python 有用的Python代码片段

Python 向 Postman 请求代码片段

python [代码片段]一些有趣的代码#sort

使用 Python 代码片段编写 LaTeX 文档

python 机器学习有用的代码片段