• Python 类 类的封装,继承 ,多继承


    __author__ = '12711'
    #-*- coding:utf-8 -*-
    # class Animal(object):
    # name='xxx'
    # def __init__(self,name,age):
    # self.name=name
    # self.age=age
    #
    # def eat(self):
    # print("%s开始吃东西了"%(self.name))
    #
    # def seelp(self):
    # print("%s开始睡觉了"%(self.name))
    #
    # t=Animal('小花',22)
    # print(Animal.name)
    # t.seelp()
    # t.eat()


    #类的封装
    # class Animal(object):
    # def __init__(self,name,age):
    # self.__name=name
    # self.age=age
    #
    # def __eat(self):
    # print("%s开始吃东西了"%(self.__name))
    #
    # def seelp(self):
    # print("%s开始睡觉了"%(self.__name))
    # t=Animal('xiaohua',22)
    # t.eat()
    # t.seelp()

    # class A:
    # def fa(self):
    # print('from A')
    # def test(self):
    # self.fa()
    #
    # class B(A):
    # def fa(self):
    # print('from B')
    #
    # b=B()
    # b.test()

    # class A:
    # def __fa(self):
    # print('from A')
    # def test(self):
    # self.__fa()
    #
    # class B(A):
    # def __fa(self):
    # print('from B')
    #
    # b=B()
    # b.test()

    #类的继承

    # class Animal(object):
    # def __init__(self,name,age):
    # self.name=name
    # self.age=age
    #
    # def Age(self):
    # print("%s已经%d岁了"%(self.name,self.age))
    #
    # def eat(self):
    # print("%s开始吃东西了"%(self.name))
    #
    # def seelp(self):
    # print("%s开始睡觉了"%(self.name))
    #
    # class Dog(Animal):
    # def __init__(self,name,age,color):
    # super(Dog,self).__init__(name,age)
    # self.color=color
    #
    # def voice(self):
    # print("%s汪汪汪的叫起来。。"%(self.name))
    #
    #
    # class Cat(Animal):
    # def __init__(self,name ,age,color):
    # super(Cat,self).__init__(name,age)
    #
    #
    # def voice(self):
    # print("%s喵喵喵的叫起来了。。"%(self.name))
    #
    #
    #
    # t1=Dog("小白",3,"白色")
    # t2=Cat("小花",2,"黑白斑点")
    # t1.seelp()
    # t1.eat()
    # t1.Age()
    # t1.voice()
    # t2.seelp()
    # t2.eat()
    # t2.Age()
    # t2.voice()

    #类的多继承
    class Animal(object):
    def __init__(self,name,age):
    self.name=name
    self.age=age

    def Age(self):
    print("%s已经%d岁了"%(self.name,self.age))

    def eat(self):
    print("%s开始吃东西了"%(self.name))

    def seelp(self):
    print("%s开始睡觉了"%(self.name))

    class Love(object):
    def frand(self,obj):
    print("%s和%s变成了朋友"%(self.name,obj.name))

    class Dog(Animal,Love):
    def __init__(self,name,age,color):
    super(Dog,self).__init__(name,age)
    self.color=color

    def voice(self):
    print("%s汪汪汪的叫起来。。"%(self.name))


    class Cat(Animal):
    def __init__(self,name ,age,color):
    super(Cat,self).__init__(name,age)


    def voice(self):
    print("%s喵喵喵的叫起来了。。"%(self.name))


    t1=Dog("小白",3,"白色")
    t2=Cat("小花",2,"黑白相间")
    t1.frand(t2)
  • 相关阅读:
    设计阶段如何画用例视图(UseCase View)
    如何进行博客中的摘要设计
    浅析Microsoft .net PetShop程序中的购物车和订单处理模块(Profile技术,异步MSMQ消息)<转>
    设计漂亮的样式表是一门艺术<轉>
    推荐几个Net开源项目
    使用Ajax和Jquery实现GridView的展开、合并
    36个引人注目JQuery导航菜单
    Nunit中如何进行事务性单元测试
    推荐Jquery 40个漂亮的导航菜单设计
    Highcharts:高交互性的javascript图表类库
  • 原文地址:https://www.cnblogs.com/hjdshizhidong/p/9812931.html
Copyright © 2020-2023  润新知