码迷,mamicode.com
首页 > 其他好文 > 详细

类 练习

时间:2018-11-14 15:34:40      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:ini   app   多继承   super   get   继承   tin   ping   ati   

#class People:
class People(object):
def __init__(self,name,age):
self.name=name
self.age=age
self.friends = []
def eat(self):
print("%s is eatting" % self.name)

def talk(self):
print(‘%s is talking‘% self.name)
def sleep(self):
print(‘%s is sleeping‘% self.name)
class Relation(object):
def make_friends(self,obj):
print(‘%s is making friends with %s‘%(self.name,obj.name))
self.friends.append(obj)
class Man(People,Relation):
def __init__(self,name,age,money):
#People.__init__(self,name,age)
super(Man,self).__init__(name,age)#等同于People.__init__(self,name,age),在多继承中方便
self.money =money
print(‘%s born with %s money‘% (self.name,self.money))

def piao(self):
print(‘%s is piao ‘% self.name)
def sleep(self):
People.sleep(self)
print(‘man is sleeping‘)
class Woman(People,Relation):
def get_birth(self):
print(‘%s is born a baby‘% self.name)


m1 = Man(‘lian‘,22,10000)
m1.eat()
m1.piao()
m1.sleep()
w1=Woman("ChenRonghua",25)
w1.get_birth()
m1.make_friends(w1)
w1.name="wang"
print(m1.friends[0].name)


================================

lian born with 10000 money
lian is eatting
lian is piao
lian is sleeping
man is sleeping
ChenRonghua is born a baby
lian is making friends with ChenRonghua
wang

类 练习

标签:ini   app   多继承   super   get   继承   tin   ping   ati   

原文地址:https://www.cnblogs.com/rongye/p/9958014.html

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