zoukankan      html  css  js  c++  java
  • [python] 在 python2和3中关于类继承的 super方法简要说明

    下面举一个例子,同样的代码使用 python2 和 python3 写的,大家注意两段程序中红色加粗的部分:

    python2的类继承使用super方法:

     1 #-*-  coding:utf-8 -*-
     2 '''
     3 Created on 2018年8月27日
     4 
     5 @author: anyd
     6 '''
     7 import random as r
     8 
     9 class Fish(object):
    10     def __init__(self):
    11         self.x = r.randint(0, 10)
    12         self.y = r.randint(0, 10)
    13             
    14     def move(self):
    15         #这里主要演示类的继承机制,就不考虑检查场景边界和移动方向的问题
    16         #假设所有鱼都是一路向西游
    17         self.x -= 1
    18         print "我的位置是:", self.x, self.y
    19 
    20 class Goldfish(Fish):
    21     pass
    22 
    23 class Carp(Fish):
    24     pass
    25 
    26 class Salmon(Fish):
    27     pass
    28 
    29 #上边几个都是食物,食物不需要有个性,所以直接继承Fish类的全部属性和方法即可
    30 #下边定义鲨鱼类,这个是吃货,除了继承Fish类的属性和方法,还要添加一个吃的方法
    31 
    32 class Shark(Fish):
    33     def __init__(self):
    34         super(Shark,self).__init__()       
    35         self.hungry = True
    36 
    37     def eat(self):
    38         if self.hungry:
    39             print "吃货的梦想就是天天有的吃^_^"
    40             self.hungry = False
    41         else:
    42             print "太撑了,吃不下了!"
    43             
    44 aa = Shark()
    45 aa.move()

    输出如下:

    我的位置是: 8 2
    

      

    python3的类继承使用super方法:

     1 #-*-  coding:utf-8 -*-
     2 '''
     3 Created on 2018年8月27日
     4 
     5 @author: anyd
     6 '''
     7 import random as r
     8 
     9 class Fish(object):
    10     def __init__(self):
    11         self.x = r.randint(0, 10)
    12         self.y = r.randint(0, 10)
    13             
    14     def move(self):
    15         #这里主要演示类的继承机制,就不考虑检查场景边界和移动方向的问题
    16         #假设所有鱼都是一路向西游
    17         self.x -= 1
    18         print ("我的位置是:", self.x, self.y)
    19 
    20 class Goldfish(Fish):
    21     pass
    22 
    23 class Carp(Fish):
    24     pass
    25 
    26 class Salmon(Fish):
    27     pass
    28 
    29 #上边几个都是食物,食物不需要有个性,所以直接继承Fish类的全部属性和方法即可
    30 #下边定义鲨鱼类,这个是吃货,除了继承Fish类的属性和方法,还要添加一个吃的方法
    31 
    32 class Shark(Fish):
    33     def __init__(self):
    34         super().__init__()       
    35         self.hungry = True
    36 
    37     def eat(self):
    38         if self.hungry:
    39             print ("吃货的梦想就是天天有的吃^_^")
    40             self.hungry = False
    41         else:
    42             print ("太撑了,吃不下了!")
    43             
    44 aa = Shark()
    45 aa.move()

    输出如下:

    我的位置是: 7 4
    

      

    super方法具体使用注意事项可以参考官方的指导文档,里面有详细的使用例子,但个人觉得这种super方法不太容易让人看得舒服,个人比较偏好采用未绑定的方法来写,这样就不管是python2 还是 python3,都是没有问题的。如下:

    1 class Shark(Fish):
    2     def __init__(self):
    3         Fish.__init__(self)     
    4         self.hungry = True
  • 相关阅读:
    数据分析项目补充:医院药品销售数据分析
    会计实务会计分录——资产之流动资产篇
    HTTP请求过程和状态响应码
    网络爬虫深究-初识HTTP和https常识
    数字分析之一元线性回归及多元线性回归
    Navicat15激活(仅供学习使用,严禁任何商业用途)
    pandas补充(其二)与matplotlib补充
    pandas模块补充
    阿里云ecs服务器公网ip除了能telnet通22端口,其他如tomcat的8080端口telnet不通,原来需要添加安全组规则
    maven依赖里redis的依赖spring-boot-starter-data-redis和spring-boot-starter-redis有什么区别?
  • 原文地址:https://www.cnblogs.com/aziji/p/9542846.html
Copyright © 2011-2022 走看看