据我所知,它是指向超类的指针.它与超类硬连接,而不是在运行时动态计算出来的.想更详细地了解它......
任何人?
我最近发现(通过StackOverflow)调用基类中的方法我应该调用:
super([[derived class]], self).[[base class method]]()
没关系,它有效.但是,当我进行更改时,我发现自己经常在类之间复制和粘贴,而且我经常忘记将派生类参数修复为super()函数.
我想避免记得更改派生类参数.我可以改为使用self.__class__
super()函数的第一个参数吗?
它似乎有用,但有充分的理由我不应该这样做吗?
我有两节课,Field
和Background
.他们看起来有点像这样:
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中,如何选择要调用哪个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)
对我来说似乎疯了.我究竟做错了什么?
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)
谁能帮我吗.
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 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)
.在大多数情况下,方法解析顺序是一个遥远的童话故事.
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代码片段
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
相应的基类的构造函数.
谢谢,
我正在使用一些具有3级继承级别的代码.从最低级派生类,调用方法2的语法是什么,层次结构升级,例如super.super调用?"中间"类没有实现我需要调用的方法.
super ×10
python ×8
inheritance ×3
python-2.7 ×3
base ×1
c# ×1
grandchild ×1
java ×1
methods ×1
objective-c ×1
overwrite ×1
python-2.x ×1
python-3.x ×1
superclass ×1