标签: super

在Objective-C中究竟是什么超级?

据我所知,它是指向超类的指针.它与超类硬连接,而不是在运行时动态计算出来的.想更详细地了解它......

任何人?

objective-c super objective-c-runtime

48
推荐指数
4
解决办法
3万
查看次数

在派生类中调用super()时,我可以传入self .__ class__吗?

我最近发现(通过StackOverflow)调用基类中的方法我应该调用:

super([[derived class]], self).[[base class method]]()

没关系,它有效.但是,当我进行更改时,我发现自己经常在类之间复制和粘贴,而且我经常忘记将派生类参数修复为super()函数.

我想避免记得更改派生类参数.我可以改为使用self.__class__super()函数的第一个参数吗?

它似乎有用,但有充分的理由我不应该这样做吗?

python super python-2.7

47
推荐指数
2
解决办法
8325
查看次数

Python,重写继承的类方法

我有两节课,FieldBackground.他们看起来有点像这样:

class Field( object ):
    def __init__( self, a, b ):
        self.a = a
        self.b = b
        self.field = self.buildField()

    def buildField( self ):
        field = [0,0,0]
        return field

class Background( Field ):
    def __init__( self, a, b, c ):
        super(Background, self).__init__( a, b )
        self.field = self.buildField( c )

    def buildField( self, c ):
        field = [c]
        return field

a, b, c = 0, 1, 2
background = Background( a, b, c )
Run Code Online (Sandbox Code Playgroud)

这个错误指向Field的buildField() …

python methods inheritance overwrite super

44
推荐指数
4
解决办法
6万
查看次数

Python的多重继承:选择要调用的super()

在Python中,如何选择要调用哪个Parent方法?假设我想调用父ASDF2的__init__方法.好像我必须在super()中指定ASDF1 ..?如果我想调用ASDF3 __init__,那么我必须指定ASDF2

>>> class ASDF(ASDF1, ASDF2, ASDF3):
    def __init__(self):
        super(ASDF1, self).__init__()


>>> ASDF()
ASDF2's __init__ happened
>>> class ASDF(ASDF1, ASDF2, ASDF3):
    def __init__(self):
        super(ASDF2, self).__init__()


>>> ASDF()
ASDF3's __init__ happened
Run Code Online (Sandbox Code Playgroud)

对我来说似乎疯了.我究竟做错了什么?

python multiple-inheritance super

43
推荐指数
3
解决办法
2万
查看次数

C#中超级关键字的等价物

super关键字(java)的等效c#关键字是什么.

我的java代码:

public class PrintImageLocations extends PDFStreamEngine
{
    public PrintImageLocations() throws IOException
    {
        super( ResourceLoader.loadProperties( "org/apache/pdfbox/resources/PDFTextStripper.properties", true ) );
    } 

     protected void processOperator( PDFOperator operator, List arguments ) throws IOException
    {
     super.processOperator( operator, arguments );
    }
Run Code Online (Sandbox Code Playgroud)

现在我在C#中究竟需要等价的超级关键字,最初尝试的base是我是否以正确的方式使用了关键字base

class Imagedata : PDFStreamEngine
{
   public Imagedata() : base()
   {                                          
         ResourceLoader.loadProperties("org/apache/pdfbox/resources/PDFTextStripper.properties", true);
   }

   protected override void processOperator(PDFOperator operations, List arguments)
   {
      base.processOperator(operations, arguments);
   }
}
Run Code Online (Sandbox Code Playgroud)

谁能帮我吗.

c# java base super

43
推荐指数
1
解决办法
9万
查看次数

super()和@staticmethod交互

super()是不是要用于staticmethods?

当我尝试类似的东西

class First(object):
  @staticmethod
  def getlist():
    return ['first']

class Second(First):
  @staticmethod
  def getlist():
    l = super(Second).getlist()
    l.append('second')
    return l

a = Second.getlist()
print a
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

Traceback (most recent call last):
  File "asdf.py", line 13, in <module>
    a = Second.getlist()
  File "asdf.py", line 9, in getlist
    l = super(Second).getlist()
AttributeError: 'super' object has no attribute 'getlist'
Run Code Online (Sandbox Code Playgroud)

如果我将staticmethods更改为classmethods并将类实例传递给super(),那么一切正常.我在这里不正确地调用超级(类型)还是有些东西我不见了?

python static-methods super python-2.7

42
推荐指数
2
解决办法
1万
查看次数

为什么在Python-2.x中打破了super()?

经常声明在Python 2 中super应该避免使用.我super在Python 2中使用它发现它永远不会按照我的预期行事,除非我提供所有参数,例如:

super(ThisClass, self).some_func(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

在我看来,这违背了使用的目的super(),它既不简洁,也不比它更好TheBaseClass.some_func(self, *args, **kwargs).在大多数情况下,方法解析顺序是一个遥远的童话故事.

python multiple-inheritance python-2.x super python-3.x

41
推荐指数
3
解决办法
5万
查看次数

Python super()行为不可靠

出于某种原因,该super()方法并不总是按预期运行,选择返回:

TypeError('super(type, obj): obj must be an instance or subtype of type)'
Run Code Online (Sandbox Code Playgroud)

我理解错误的含义.我不明白为什么它会出现错误.这是破解的代码片段.系统中的所有对象都是新样式对象.

真正有趣的是,这个错误并不总是出现.我不知道是什么导致了它.该super()在方法Retrieval中传递Retrieval类,然后本身作为一个对象,它是,据我所知,究竟如何super()应该被调用.

有什么想法吗?

在文件DBConnection.py中:

class DBAdminConnection(object):
    def __init__(self):
        self.user = DBUserConnection().user 
        self.submissions = DBSubmissionConnection()
Run Code Online (Sandbox Code Playgroud)

在文件Retrieval.py中

class Retrieval(DBConnection.DBAdminConnection): 
    def __init__(self, username=None, password=None, unique_key=None):
        super(Retrieval,self).__init__()
        if username and password:
            self.username = username
            self.user.login(username,password, config.DATABASE)
            if self.user.error:
                raise UserLoginError(username)
        self.unique_key = unique_key
Run Code Online (Sandbox Code Playgroud)

python inheritance super superclass

41
推荐指数
2
解决办法
2万
查看次数

python多继承使用super将构造函数传递给参数

考虑下面的python代码片段

class A(object):
    def __init__(self, a):
        self.a = a

class B(A):
    def __init__(self, a, b):
        super(B, self).__init__(a)
        self.b = b

class C(A):
    def __init__(self, a, c):
        super(C, self).__init__(a)
        self.c = c

class D(B, C):
    def __init__(self, a, b, c, d):
        #super(D,self).__init__(a, b, c) ???
        self.d = d
Run Code Online (Sandbox Code Playgroud)

我想知道如何传递a,b以及c相应的基类的构造函数.

谢谢,

python multiple-inheritance super diamond-problem

41
推荐指数
3
解决办法
2万
查看次数

如何从孙子类中调用超级方法?

我正在使用一些具有3级继承级别的代码.从最低级派生类,调用方法2的语法是什么,层次结构升级,例如super.super调用?"中间"类没有实现我需要调用的方法.

python inheritance super grandchild python-2.7

40
推荐指数
3
解决办法
6万
查看次数