码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-05-22 18:19:27      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:def   any   day   print   people   format   ini   form   一段   

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月,安徽安庆人

后面再学了再贴.

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

标签:def   any   day   print   people   format   ini   form   一段   

原文地址:https://www.cnblogs.com/zhangziyan/p/9073088.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!