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 经典类和新式类都是统一按广度优先来继承的
  • 相关阅读:
    2021.2.28
    《构建之法》11~16章读后感
    《构建之法》6~10章读后感
    《构建之法》1~5章读后感
    4.7 wait notify
    4.8 wait,notify 的正确姿势
    4.9 park&unpark
    4.10 重新理解线程的状态转换
    第七章 Redis-6.2.1脚本安装
    第三十九章 Centos 7 系统优化脚本
  • 原文地址:https://www.cnblogs.com/rongye/p/9958014.html
Copyright © 2011-2022 走看看