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
  • 相关阅读:
    JavaScript设计模式(策略模式)
    JavaScript设计模式(单例模式)
    react中数据持久化缓存redux-persist
    webpack编写一个plugin插件
    webpack自定义loader并发布
    Vue用递归实现一个消除输入框表情符的自定义directive
    Spring MVC表单防重复提交
    @Controller,@Service,@Repository,@Component详解
    Spring Enable*高级应用及原理
    Spring Aware容器感知技术
  • 原文地址:https://www.cnblogs.com/aziji/p/9542846.html
Copyright © 2011-2022 走看看