首页 > 其他 > 详细

类 练习

时间:2018-11-14 15:12:46      阅读:140      评论:0      收藏:0      [点我收藏+]
#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

类 练习

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!