python中类和继承,继承还不太了解,先贴一段代码

python中的类和继承,初步认识,其中第12行calss people()中加不加company都是一样的,这点还不了解.

class company:
    def __init__(self,name,birthyear,birthmonth,birthplace):
        self.name=name
        self.birthyear=birthyear
        self.birthmonth=birthmonth
        self.birthplace=birthplace
    def desc(self):
        print('%s创建于%d年%d月,诞生于{0}'.format(self.birthplace)%(self.name,self.birthyear,self.birthmonth))
        print('{0}创建于{1}年{2}月,诞生于{3}'.format(self.name,self.birthyear,self.birthmonth,self.birthplace))
        print(self.name,'创建于',self.birthyear,'',self.birthmonth,'','诞生于',self.birthplace)

class people(company):
    def __init__(self,name,birthday,birthmonth,birthplace):
        company.__init__(self,name,birthday,birthmonth,birthplace)
    def desc(self):
        print('{0}出生于{1}年{2}月,{3}人'.format(self.name, self.birthyear, self.birthmonth, self.birthplace))

c=company('博客园',2004,1,'江苏扬州')
c.desc()
p=people('陈独秀',1879,10,'安徽安庆')
p.desc()

运行结果:

博客园创建于2004年1月,诞生于江苏扬州
博客园创建于2004年1月,诞生于江苏扬州
博客园 创建于 2004 年 1 月 诞生于 江苏扬州
陈独秀出生于1879年10月,安徽安庆人

后面再学了再贴.

猜你喜欢

转载自www.cnblogs.com/zhangziyan/p/9073088.html
今日推荐