请参阅oper http://perldoc.perl.org/perlobj.html上的perldoc
根据文档:"重要的是要注意SUPER指的是当前包的超类,而不是指对象的超类."
现在,我处于这样一种情况,我需要SUPER来引用对象的超类.
所以,寻找任何方法来实现它.
在下面的示例中,如何从类C的方法method()中访问A?
class A {
public void method() { }
}
class B extends A{
public void method() { }
}
class C extends B{
public void method() { }
void test() {
method(); // C.method()
super.method(); // B.method()
C.super.method(); // B.method()
B.super.method(); // ERROR <- What I want to know
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
在范围内不能访问类型B的封闭实例
答:不,这是不可能的.Java不允许它.类似的问题.
如果我public void send() { /* some code */ }在类中有方法并且让这个类的子类也有一个方法public void send() { /* some code*/ },我如何确保子句必须在它试图覆盖的send()方法中的某处调用super.send()?
我想知道这个,因为我已经用API写了,如果你在覆盖它时不调用那个方法的超级,它会抛出一个异常告诉我,我没有调用超级方法.这是硬编码还是可以用Java中的一些关键字来完成?
在我正在阅读的Objective-C书中,据说当[init]发送消息时NSObject,有时它可能会返回nil,我们应该检查返回值,然后再将更多消息发送到可能最终成为的消息nil.
self = [super init];
if (self) {
do stuff
}
Run Code Online (Sandbox Code Playgroud)
我问你,虽然,需要采取什么措施对于NSObject不能够init自己?
编辑:问题专门处理YourClass:NSObject的实例.
我的问题是合并两种技术:
假设一个具有递归函数(foo)的根类和一个覆盖此函数的扩展类(foo):override函数必须调用super :: foo,但需要在递归调用之前执行其他操作.
我将尝试一个例子(这只是一个例子,我知道有非递归的方法来解决这个问题)
class Node
{
public:
// must be override
virtual int getNumValues()
{
if (parent) return parent->getNumValues() + 3;
else return 3;
}
protected:
Node *parent;
private:
int values[3];
};
class ExtNode: Node
{
public:
//@override
virtual int getNumValues()
{
int aux = Node::getNumValues(); //but need to avoid recursion here.
if (parent) return parent->getNumValues() + aux + 2;
else return aux + 2;
}
private:
int extValues[2];
};
Run Code Online (Sandbox Code Playgroud)
所以我想要的是:
我正在尝试一些想法,但它们似乎很糟糕的编程实践或不可能:
// …Run Code Online (Sandbox Code Playgroud) 究竟是为什么
A.__init__()
B.__init__()
D.__init__()
Run Code Online (Sandbox Code Playgroud)
由以下代码打印?特别是:
为什么C.__init__() 不打印?
为什么C.__init__()打印super().__init__()而不是A.__init__(self)?
#!/usr/bin/env python3
class A(object):
def __init__(self):
super(A, self).__init__()
print("A.__init__()")
class B(A):
def __init__(self):
A.__init__(self)
print("B.__init__()")
class C(A):
def __init__(self):
A.__init__(self)
print("C.__init__()")
class D(B, C):
def __init__(self):
super(D, self).__init__()
print("D.__init__()")
D()
Run Code Online (Sandbox Code Playgroud) python multiple-inheritance super diamond-problem python-3.x
这是super()的合法使用吗?
class A(object):
def method(self, arg):
pass
class B(A):
def method(self, arg):
super(B,self).method(arg)
class C(B):
def method(self, arg):
super(B,self).method(arg)
Run Code Online (Sandbox Code Playgroud)
谢谢.
为什么这样设计?
Ruby的设计倾向于尽可能多地实现方法; 关键字通常保留用于具有自己的语法规则的语言功能.super但是,外观和行为就像一个方法调用.
(我知道super在纯Ruby中实现它会很麻烦,因为它必须解析方法名称caller,或者使用trace_func.仅此一项不会阻止它成为一种方法,因为很多内核的方法都是没有在纯Ruby中实现.)
这个问题与What does 'super' do in Python? 的帖子有关。,如何初始化基(超)类?和Python:如何从超类创建子类?SuperClass它描述了从SubClassas内部初始化 a 的两种方法
class SuperClass:
def __init__(self):
return
def superMethod(self):
return
## One version of Initiation
class SubClass(SuperClass):
def __init__(self):
SuperClass.__init__(self)
def subMethod(self):
return
Run Code Online (Sandbox Code Playgroud)
或者
class SuperClass:
def __init__(self):
return
def superMethod(self):
return
## Another version of Initiation
class SubClass(SuperClass):
def __init__(self):
super(SubClass, self).__init__()
def subMethod(self):
return
Run Code Online (Sandbox Code Playgroud)
所以我对需要在
and
中显式传递self参数
感到有点困惑。(事实上,如果我打电话我会得到错误SuperClass.__init__(self)super(SubClass, self).__init__()SuperClass.__init__()
TypeError: __init__() missing 1 required positional argument: …Run Code Online (Sandbox Code Playgroud) 我理解这__new__是一个静态方法,super()可以从它调用创建一个新对象,如下所示:
>>> class A:
... def __new__(cls):
... print('__new__ called')
... return super().__new__(cls)
...
>>> a = A()
__new__ called
Run Code Online (Sandbox Code Playgroud)
为什么super调用不能与其他静态方法一起使用?为什么以下失败?
>>> class B:
... @staticmethod
... def funcB():
... print('funcB called')
...
>>> class C(B):
... @staticmethod
... def funcC():
... print('funcC called')
... super().funcB()
...
>>> c = C()
>>> c.funcC()
funcC called
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in funcC
RuntimeError: super(): …Run Code Online (Sandbox Code Playgroud) super ×10
python ×4
java ×2
python-3.x ×2
superclass ×2
c++ ×1
inheritance ×1
init ×1
methods ×1
objective-c ×1
oop ×1
overriding ×1
perl ×1
recursion ×1
ruby ×1
self ×1
subclass ×1