类 练习

#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

猜你喜欢

转载自www.cnblogs.com/rongye/p/9958014.html