问题标签 [super]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - 在 Java 类型参数中,是否仅表示严格的子类型?还是E也足够了?
在 Java 类型参数中,是否仅表示严格的子类型?还是E也足够了?
ruby - 我如何调用祖父母的方法,并在 ruby 中跳过父母
如何在继承链中选择特定的 a 方法调用?
因此输出将是 A:Boo, C:Boo
TIA,
-丹尼尔
python - Django form.save 一步一步
假设我有一个用于添加/编辑产品的表单(字段“用户”是我的用户的外键),由两个单独的视图函数触发 - 添加/编辑:
表单的保存方法如下所示:
有人可以一步一步告诉我这个保存方法发生了什么吗?为什么我们在这个表单上调用 super 以及如果函数调用相同,当我们编辑和保存新产品时整个处理有什么区别?
python - Python:你如何记住 `super` 参数的顺序?
正如标题所说,你如何记住super
's 参数的顺序?有没有我错过的助记符?
经过多年的 Python 编程,我仍然需要查找它:(
(为了记录,它是super(Type, self)
)
java - 对超级关键字的困惑;用 Java 实现的装饰器模式
上面的解释非常好。
但是,我对 Decorator Pattern (DeP) 的实现感到有些困惑,如
上述链接代码的设计在 tinypic.com/view.php?pic=xnaqlt&s=3
我对“super.callTrailer();”感到困惑 在装饰器类 Header1、Header2、Footer1 和 Footer2 中,都派生自 TicketDecorator。
不应该只是“callTrailer();”吗?? 由于“private Component myTrailer;”这一行,每个装饰器对象都有自己对下一个装饰器的引用。
注意:我不是很精通 Java,也是设计模式的初学者。
python - (Python) Closure created when it wasn't expected
I got an unexpected closure when creating a nested class. I suspect that this is something related to metaclasses, super, or both. It is definitely related to how closures get created. I am using python2.7.
Here are five simplified examples that demonstrate the same problem that I am seeing (they all build off the first):
EXAMPLE 1:
EXAMPLE 2:
EXAMPLE 3:
EXAMPLE 4:
EXAMPLE 5:
Here is what I understand (referencing examples):
- Metaclasses are inherited, so
Subclass
getsBase
’s metaclass. - Only
__init__
is affected,Subclass.other
method is not (#1). - Removing
Subclass.other
does not make a difference (#1). - Removing
self.name=name
fromSubclass.__init__
does not make a difference (#1). - The object in the closure cell is not a function.
- The object is not
Metaclass
orBase
, but some object of typeMetaclass
, just likeBase
is (#1). - The object is actually an object of the type of the nested
Subclass
(#1). - The closure cells for
t1.subclass.__init__
andt1.subclass2.__init__
are the same, even though they are from two different classes (#1). - When I do not nest the creation of
Subclass
(#1) then there is no closure created. - When I do not call
super(...).__init__
inSubclass.init__
no closure is created (#2). - If I assign no
__metaclass__
and inherit fromobject
then the same behavior shows up (#3). - The object in the closure cell for
t3.other.__init__
ist3.other
(#3). - The same behavior happens if the metaclass has no
__init__
(#4). - The same behavior happens if the
Base
has no__init__
(#4). - The closure cells for the three subclasses in example 4 are all different and each matches the corresponding class (#4).
- When
super(...).__init__
is replaced withBase.__init__(self)
, the closure disappears (#5).
Here is what I do not understand:
- Why does a closure get set for
__init__
? - Why doesn't the closure get set for other?
- Why is the object in the closure cell set to the class to which
__init__
belongs? - Why does this only happen when
super(...).__init__
is called? - Why doesn't this happen when
Base.__init__(self)
is called? - Does this actually have anything at all to do with using metaclasses (probably, since the default metaclass is
type
)?
Thanks for the help!
-eric
(Update) Here is something that I found then (based on Jason's insight):
First, a function's name is in scope within its own body. Second, functions get closures for the functions in which they are defined if they reference those functions.
I hadn't realized that the function name was in scope like that. The same goes for classes. When a class is defined within a function's scope, any references to that class name inside the class's methods cause the class to bound in a closure on that method's function, like so:
However, since closures cannot be created on non-functions the following fails:
Good stuff!
java - 从超类中获取子类的名称
假设我有一个名为Entity
. 在那个类中,我有一个静态方法来检索类名:
现在我有另一个类扩展它。
我想获取用户的类名:
我的目标是看到控制台的“com.packagename.User”输出,但我最终会得到“com.packagename.Entity”,因为实体类是直接从静态方法引用的。
如果这不是静态方法,则可以通过this
在类中使用关键字Entity
(即:)轻松解决return this.class.getClass()
。但是,我需要这种方法保持静态。关于如何解决这个问题的任何建议?
python - 通过子类化修改命名元组的构造函数参数?
我想创建一个namedtuple
代表短位域中的各个标志的。我正在尝试对其进行子类化,以便在创建元组之前解压缩位域。但是,我目前的尝试不起作用:
现在,我的经验super()
是有限的,而且我的经验__new__
几乎不存在,所以我不太确定(对我来说)神秘的错误该怎么办TypeError: super.__new__(Status): Status is not a subtype of super
。谷歌搜索和挖掘文档并没有产生任何启发性。
帮助?
java - Java中的super()
是super()
用来调用父构造函数的吗?请解释super()
。
python - 调用变量超类方法
我试图通过使用变量方法名称来调用超类的方法。通常,我会看到以下两行代码是等效的:
事实上,我相信,这也是我使用第一行时实际发生的情况。然而,在下面的例子中,第二行产生了一个奇怪的问题。
我super
用来构造一个超对象并调用超类的方法。直接执行它按预期工作,但是使用__getattribute__
首先获取方法会导致无限循环,它会一次又一次地调用子类的方法。
请参阅以下代码:
如果您运行该代码,一切都会按预期进行,并且您应该得到类似于以下内容的输出:
所以这两种方法,一种是直接访问,另一种是 via __getattribute__
,看起来是一样的。但是,如果您用注释掉的行替换方法调用,您最终会遇到递归运行时错误。
为什么会发生这种情况,更重要的是,当我使用工作行时,如何以与 python 内部相同的方式实际访问该方法?
编辑
当我以为我已经尝试了所有方法时,我发现这是可行的:
它实际上等于super( B, self ).example
。