zoukankan      html  css  js  c++  java
  • Python自学之乐-继承的新式类与经典类

    #Author:clark
    class Animal(object):#python3中新式类写法,继承object(所有类的基类)
    #kind = "" #类属性
    def __init__(self,name,age,food):#name等是实例属性 init方法叫做构造方法__del__是析构方法
    self.Name = name
    self.Age = age
    self.food = food
    def eat(self):
    print("%s is eat %s" %(self.Name,self.food))
    def barking(self):
    print("%s is barking"%self.Name)

    class dog(Animal): #狗是动物的子类,如果增加新的属性并且要继承父类的属性,按照下面这样去写
    def __init__(self,name,age,food,kind):
    #Animal.__init__(self,name,age,food) 经典类的写法,这种写法比较繁琐,如果该类继承多个类的相同属性,就需要每个类都要写,新式类使用super写法,写一个就行
    super(dog,self).__init__(name,age,food)
    self.kind = kind
    def barking(self):
    print("the barking of %s is wangwangwang!"%self.Name)
    class cat(Animal):
    def __init__(self,name,age,food,kind):
    #Animal.__init__(self,name,age,food)
    super(cat,self).__init__(name,age,food)
    self.kind = kind
    def barking(self):
    print("the barking of %s is miaomiaomiao"%self.Name)

    dog_teddy = dog("ahuang",3,"bone","teddy")
    dog_teddy.eat()
    cat_xiaohua = cat("xiaohua",2,"fish","cat")
    cat_xiaohua.eat()
    dog_teddy.barking()
    cat_xiaohua.barking()

    运行结果为:

    ahuang is eat bone
    xiaohua is eat fish
    the barking of ahuang is wangwangwang!
    the barking of xiaohua is miaomiaomiao

  • 相关阅读:
    django 查询
    django orm
    django 基础
    RFS自动化测试工具安装与使用总结
    虚拟机安装(Cent OS)
    RobotFramework 切换窗口控制的用法小结
    Selenium2Library中select frame关键字对没有name和id的frame或者iframe的处理
    robotframework+selenium2library之上传本地文件
    element not visible的解决方法
    robotframework悬浮菜单定位问题
  • 原文地址:https://www.cnblogs.com/clarkxhb/p/7536065.html
Copyright © 2011-2022 走看看