精华内容
下载资源
问答
  • 测试环境:win7 64位Python版本:Python 3.3.5代码实践:1、在子类中通过“类名”调用父类方法class FatherA: def __init__(self): print('init action in father class A')class SubClassB(FatherA): def __init_...

    测试环境:

    win7 64位

    Python版本:Python 3.3.5

    代码实践:

    1、在子类中通过“类名”调用父类的方法

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in subclass B')
    
            FatherA.__init__(self) # 在子类中调用父类的方法:父类名.方法名称(参数)
    
    
    
    if __name__ == '__main__':
    
        b = SubClassB()
    
    

    运行结果:

    >>>
    
    init action in subclass B
    
    init action in father class A

    缺点:当一个子类的父类发生变化时(如类SubClassB的父类由FatherA变为FatherD时),必须遍历整个类定义,把子类中所有的父类类名全部替换过来

    2、在子类中通过“super”方法调用父类的方法

    场景1、单层继承

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in father class B')
    
            super().__init__() # 在子类中调用父类的方法:super().方法名称(参数)
    
    
    
    if __name__ == '__main__':
    
        b = SubClassB()
    
    
    
    
    
    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in subclass B')
    
            super(SubClassB, self).__init__()  # 在子类中调用父类的方法:super(type, obj).方法名称(参数)
    
    
    
    
    
    if __name__ == '__main__':
    
        b = SubClassB()
    
    

    运行结果:

    >>>
    
    init action in father class B
    
    init action in father class A

    说明:

    1、super(type, obj),其中obj必须是type类型、type子类类型的实例,否则会报错:

    TypeError: super(type, obj): obj must be an instance or subtype of type

    2、super().init() 效果等同 super(SubClassB, self). init()

    场景2、多层继承

    实验1:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in subclass B')
    
            super().__init__()
    
    
    
    class SubClassC(SubClassB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super().__init__()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    
    

    运行结果:

    >>>
    
    init action in subclass C
    
    init action in subclass B
    
    init action in father class A

    对比实验1-1:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in subclass B')
    
            super().__init__()
    
    
    
    class SubClassC(SubClassB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super(SubClassB, self).__init__()
    
    
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    

    运行结果:

    
    >>>
    
    init action in subclass C
    
    init action in father class A
    
    >>> 

    对比实验1-2:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in subclass B')
    
            super().__init__()
    
    
    
    class SubClassC(SubClassB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super(FatherA, FatherA).__init__(self) # 在子类中调用父类的方法:super(type, type).方法名称(参数)
    
    
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    

    说明:可把 super(FatherA, FatherA).init(self) 换成:

    super(FatherA, self).init()

    super(FatherA, SubClassB).init(self)

    super(FatherA, SubClassC).init(self)

    注意:以上这种情况,必须给init传递参数self,否则会报错:

    TypeError: init() missing 1 required positional argument: ‘self’

    运行结果:

    >>>
    
    init action in subclass C
    
    >>> 

    说明:super(type1, type2) ,type2必须和type1相同类型,或者其子类类型

    实验2:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in subclass B')
    
            super().__init__()
    
    
    
    class SubClassC(SubClassB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super().__init__()
    
    
    
    class SubClassD(SubClassC):
    
        def __init__(self):
    
            print('init action in subclass D')
    
            super().__init__()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassD()
    
    

    运行结果:

    
    >>>
    
    init action in subclass D
    
    init action in subclass C
    
    init action in subclass B
    
    init action in father class A
    

    对比实验2-1:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in subclass B')
    
            super().__init__()
    
    
    
    class SubClassC(SubClassB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super().__init__()
    
    
    
    class SubClassD(SubClassC):
    
        def __init__(self):
    
            print('init action in subclass D')
    
            super(SubClassB, self).__init__()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassD()

    运行结果:

    
    >>>
    
    init action in subclass D
    
    init action in father class A
    
    >>> 

    对比实验2-2:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class SubClassB(FatherA):
    
        def __init__(self):
    
            print('init action in subclass B')
    
            super().__init__()
    
    
    
    class SubClassC(SubClassB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super().__init__()
    
    
    
    class SubClassD(SubClassC):
    
        def __init__(self):
    
            print('init action in subclass D')
    
            super(SubClassC, self).__init__()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassD()
    
    

    运行结果:

    
    >>>
    
    init action in subclass D
    
    init action in subclass B
    
    init action in father class A

    通过对比实验2-1, 2-2,可看出super(type[,type2_or_obj]),type决定了super调用方法所在的父类–type的父类(如果有的话),即type决定了前往哪个父类调用指定的方法

    场景3、多重继承

    实验1:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class FatherB:
    
        def __init__(self):
    
            print('init action in father class B')
    
    
    
    class SubClassC(FatherA, FatherB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super(FatherB).__init__()
    
    
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    

    运行结果:

    
    >>>
    
    init action in subclass C
    
    

    对比实验1-1:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class FatherB:
    
        def __init__(self):
    
            print('init action in father class B')
    
    
    
    class SubClassC(FatherB, FatherA):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super().__init__()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    

    运行结果:

    
    >>>
    
    init action in subclass C
    
    init action in father class B
    
    >>> 
    

    对比实验1-2:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class FatherB:
    
        def __init__(self):
    
            print('init action in father class B')
    
    
    
    class SubClassC(FatherA, FatherB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super().__init__()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    

    运行结果:

    
    >>>
    
    init action in subclass C
    
    init action in father class A
    
    >>> 
    

    对比实验1-3:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class FatherB:
    
        def __init__(self):
    
            print('init action in father class B')
    
    
    
    class SubClassC(FatherA, FatherB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super(FatherB).__init__()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    >>> ================================ RESTART ================================
    
    >>>
    
    init action in subclass C

    对比实验1-4:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
        def testfn(self, arg):
    
            print('testfn in father class A')
    
    
    
    class FatherB:
    
        def __init__(self):
    
            print('init action in father class B')
    
    
    
        def testfn(self):
    
            print('testfn in father class B')
    
    
    
    class SubClassC(FatherA, FatherB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super().testfn()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()

    运行结果:

    
    >>>
    
    init action in subclass C
    
    Traceback (most recent call last):
    
      File "C:/Users/Administrator/Desktop/1.py", line 21, in
    
        b = SubClassC()
    
      File "C:/Users/Administrator/Desktop/1.py", line 18, in __init__
    
        super().testfn()
    
    TypeError: testfn() missing 1 required positional argument: 'arg'
    
    >>> 
    

    对比实验1-5:

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
        def testfn(self):
    
            print('testfn in father class A')
    
    
    
    class FatherB:
    
        def __init__(self):
    
            print('init action in father class B')
    
    
    
        def testfn(self, arg):
    
            print('testfn in father class B')
    
    
    
    class SubClassC(FatherA, FatherB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            super().testfn()
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    

    运行结果:

    
    >>>
    
    init action in subclass C
    
    testfn in father class A
    
    

    说明:通过对比实验1-1,1-2,1-3,1-4,1-5可以看出,子类水平方向上,继承多个父类,以super().method(参数)方法调用父类的方法,如果不同父类中存在同名方法method(不管参数列表是否相同),则按继承顺序,选择第一个父类中的方法。,如果想要调用多个方法咋办?如下,通过类名调用

    class FatherA:
    
        def __init__(self):
    
            print('init action in father class A')
    
    
    
    class FatherB:
    
        def __init__(self):
    
            print('init action in father class B')
    
    
    
    class SubClassC(FatherA, FatherB):
    
        def __init__(self):
    
            print('init action in subclass C')
    
            FatherA.__init__(self)
    
            FatherB.__init__(self)
    
    
    
    if __name__ == '__main__':
    
        b = SubClassC()
    
    
    >>>
    
    init action in subclass C
    
    init action in father class A
    
    init action in father class B
    
    >>> 
    
    展开全文
  • Python在子类中调用父类方法super

    千次阅读 2019-07-25 19:25:27
    2、改进一下,在子类中调用父类的init方法 3、在子类里面也调用一下父类的run方法 4、super方法 调用父类方法 super的方法的好处是,当你的父类的名字改变之后,只需要Subway那里继承的名字改一下,...

    1、没有super之前,在子类里面需要父类里面的逻辑,但是我们是通过派生(自己定义了一个init,增加了一条line).

     

    2、改进一下,在子类中调用父类的init方法

     

    3、在子类里面也调用一下父类的run方法

     

    4、super方法   调用父类的方法

    super的方法的好处是,当你的父类的名字改变之后,只需要在Subway那里继承的名字改一下,就行,不用动下面的逻辑

    第一种写法:

     

    我们记住上面的第一种就行了,有的人写第二种我们也要能看懂

     

    展开全文
  • 子类中调用父类方法

    2019-05-24 23:22:00
    可以用中文做变量 ...super调用父类方法:super().__init__(name,speed),不用传self,更爱父类名补收影响 class Dad1: def zou(self): print(123) class Son(Dad1): def _zou(self): su...

    可以用中文做变量

    实例化和对象调用方法会自动传递self

    super调用父类方法:super().__init__(name,speed),不用传self,更爱父类名补收影响

     

    class Dad1:
        def zou(self):
            print(123)
    class Son(Dad1):
        def _zou(self):
            super(Son,self).zou()
            print(456)
    m=Son()
    m.zou()
    print(m.__class__)
    子类调用父类方法示例

     

     

     

    转载于:https://www.cnblogs.com/jintian/p/10920733.html

    展开全文
  • 子类如何调用父类方法

    千次阅读 2017-02-14 15:56:59
    一'子类继承父类 并重写了父类方法调用父类方法 1 zi() z= new fu();  z.f类方法();或者super.父类方法(); //注意 super不能用static修饰的方法中 如果父类非abstract class 也可以用fu f = ...

    一'子类继承父类 并重写了父类方法调用父类方法

    1 zi() z= new   fu();

      z.f类方法();或者super.父类方法();  //注意  super不能用在static修饰的方法中

    如果父类非abstract class

    也可以用fu f = new fu(); f.方法();

    二`如果子类未重写父类方法 以上均可

    zi() z =new zi() ; z.父类方法();

    如果重写了父类方法

    zi()  z = new zi();z.方法();     调用的是子类重写的方法

    三`在子类作用域内 父类不能调用子类的成员变量

    public class Fu{
            String  test () {
        return "父类的test方法";
    }
    }
    public class Zi extends  Fu {
          String   test(){ 
    return  "子类覆盖test方法";
    }
           String  test (){
    return  "子类test方法";
    }
           public static void main(String  [] args){
                  Fu f  = new  Zi();
                   f.test();   //报错 父类不能调用子类特有的方法,但可以调用从父类继承的test方法
    }
    }




    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 423,673
精华内容 169,469
关键字:

在子类中调用父类的方法