zoukankan      html  css  js  c++  java
  • super方法的使用问题

    class A:
        def __new__(cls, name):
            print("A.__new__called")
            return super().__new__(cls,name)
        def __init__(self,name):
            self.name = name
            print ("A.__init__ called")
    
        def __str__(self):
            return self.name
    s= A("xxx")
    print(s)
    
    
    class B:
        def __new__(cls, *args, **kwargs):
            print("B.__new__ called")

    错误信息

    Traceback (most recent call last):
    .....
        return super().__new__(cls.name)
    AttributeError: type object 'A' has no attribute 'name'
    A.__new__called

    新式类与旧式类 super继承方式不同

    更改方法

    class A:
        def __new__(cls,name):
            print("A.__new__called")
            return super().__new__(cls)
        def __init__(self,name):
            self.name = name
            print ("A.__init__ called")
    
        def __str__(self):
            return self.name
    s= A("xxx")
    print(s)
    
    
    class B:
        def __new__(cls, *args, **kwargs):
            print("B.__new__ called")
  • 相关阅读:
    实习第十天
    实习第九天
    实习第八天
    武汉第七天
    武汉第六天
    实习第五天
    实习第四天
    NSArray
    NSString
    NSObject
  • 原文地址:https://www.cnblogs.com/wangxishan/p/6596502.html
Copyright © 2011-2022 走看看