zoukankan      html  css  js  c++  java
  • 类 练习

    #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

    继承
    py2 经典类是按深度优先来继承的,新式类是按广度优先来继承的
    py3 经典类和新式类都是统一按广度优先来继承的
  • 相关阅读:
    BZOJ3196: Tyvj 1730 二逼平衡树
    (转载)你真的会二分查找吗?
    Codeforces Round #259 (Div. 2)
    BZOJ1452: [JSOI2009]Count
    BZOJ2733: [HNOI2012]永无乡
    BZOJ1103: [POI2007]大都市meg
    BZOJ2761: [JLOI2011]不重复数字
    BZOJ1305: [CQOI2009]dance跳舞
    挖坑#4-----倍增
    BZOJ1042: [HAOI2008]硬币购物
  • 原文地址:https://www.cnblogs.com/rongye/p/9958014.html
Copyright © 2011-2022 走看看