标签: super

扩展通用抽象类和正确使用超级

public abstract class AbstractTool<AT extends AbstractThing> {
    protected ArrayList<AT> ledger;
    public AbstractTool() {
        ledger = new ArrayList<AT>();
    }

    public AT getToolAt(int i) {
        return ledger.get(i);
    }

    // More code Which operates on Ledger ...

}

public class Tool<AT extends AbstractThing> extends AbstractTool {
    public Tool() {
        super();
    }
}
Run Code Online (Sandbox Code Playgroud)

如何正确调用super将AT泛型传递Tool给AbstractTool构造函数?

AT当我宣布Tool(Say,Tool<Thing>)时,似乎无论我选择什么,我总是回来AbstractThing而不是Thing.这似乎打败了仿制药的目的......

救命?

java generics super

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

确定在Python 3中super().__ new__是否为object .__ new__?

假设我有一些调用的类__new__,如何__new__根据需要调用mro并调用超类(带参数),但不调用object.__new__其他参数?例如,只有在没有向构造函数传递参数时才有效:

class A(object):
    def __new__(cls, *args, **kwargs):
        print("A.__new__ called")
        return super(A, cls).__new__(cls, *args, **kwargs)

class B(object):
    def __new__(cls, *args, **kwargs):
        print("B.__new__ called")
        return super(B, cls).__new__(cls, *args, **kwargs)

class C(A, B): pass

>>> C.mro()
[__main__.C, __main__.A, __main__.B, builtins.object]
>>> C()
A.__new__ called
B.__new__ called
Run Code Online (Sandbox Code Playgroud)

但是使用参数调用会导致它失败(因为在某些时候Python改为object.__new__除了调用者类之外不接受任何参数):

>>> C(1)
A.__new__ called
B.__new__ called
Traceback (most recent call last):
   ...
TypeError: object() takes no parameters
Run Code Online (Sandbox Code Playgroud)

那怎么能AB告诉他们的超级班object呢?我应该做点什么super(A, cls).__new__ is object.__new__ …

python super python-3.x

11
推荐指数
1
解决办法
1394
查看次数

What is a sibling class in Python?

Python 2 documentation says that super() function "returns a proxy object that delegates method calls to a parent or sibling class of type."

The questions:

  1. What is a sibling class in Python?
  2. How do you delegate a method call to a sibling class?

我的假设是给定类的兄弟是一个继承自同一父母的类.我起草了以下代码,以了解如何将方法调用委托给兄弟,但它不起作用.我做什么或理解错了什么?

class ClassA(object):
    def MethodA(self):
        print "MethodA of ClassA"

class ClassB(ClassA):
    def MethodB(self):
        print "MethodB of ClassB"

class ClassC(ClassA):
    def MethodA(self):
        super(ClassC, self).MethodA()

    def MethodB(self):
        super(ClassC, self).MethodB()

if __name__ == '__main__':
    ClassC().MethodA() # Works as expected

    # …
Run Code Online (Sandbox Code Playgroud)

python oop inheritance super siblings

11
推荐指数
2
解决办法
3260
查看次数

如何在Java中访问超级超类?[里面的小例子]

在下面的示例中,如何从类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不允许它.类似的问题.

java super superclass

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

如何通过手动填充__class__单元格来使super()工作?

在Python 3中,可以使用super()而不是super(MyClass, self),但这仅适用于在类中定义的方法.如Michele Simionato的文章所述,以下示例不起作用:

def __init__(self):
    print('calling __init__')
    super().__init__()

class C(object):
    __init__ = __init__

if __name__ == '__main__':
    c = C()
Run Code Online (Sandbox Code Playgroud)

它失败是因为super()查找了一个在这种情况下未定义的__class__ 单元格.

是否可以在定义功能后手动设置此单元格,还是不可能?

不幸的是,我不明白细胞是如何在这种情况下工作的(没有找到很多文档).我希望有类似的东西

__init__.__class_cell_thingy__ = C
Run Code Online (Sandbox Code Playgroud)

当然我只会在类赋值明确/唯一的情况下使用它(在我的情况下,将类添加到类的整个过程是自动化的,所以添加这样的行很简单).

python metaprogramming super python-3.x

10
推荐指数
1
解决办法
2135
查看次数

在python中'返回的'超级对象'是什么意思?

根据http://docs.python.org/2/library/functions.html#super,

如果省略第二个参数,则返回的超级对象是未绑定的.

哪个是超级(类型).

我想知道什么是无限的,什么时候有限.

python class super

10
推荐指数
2
解决办法
1188
查看次数

差异为<?方法和变量声明中的super/extends String>

鉴于:

import java.util.*;

public class Hancock {
    //insert code here
        list.add("foo");
    }
}
Run Code Online (Sandbox Code Playgroud)

在第5行独立插入的哪两个代码片段将在没有警告的情况下编译?(选择两个)

A. public void addString(List list) {
B. public void addString(List<String> list) {
C. public void addString(List<? super String> list) {
D. public void addString(List<? extends String> list) {
Run Code Online (Sandbox Code Playgroud)

正确答案是B&C.

答案A和B对我来说非常清楚.对于答案C&D我知道继承的方式,但是我无法理解为什么答案D不能在Eclipse中编译,而所有其他人都这样做(A有关于通用的警告,B&C没有warrings).

Eclipse中答案D的错误是The method add(capture#1-of ? extends String) in the type List<capture#1-of ? extends String> is not applicable for the arguments (String).

另一方面,这编译:

public void addString() {
    List<? extends String> list1 = new ArrayList<String>();
    List<? super …
Run Code Online (Sandbox Code Playgroud)

java generics collections extends super

10
推荐指数
1
解决办法
4615
查看次数

Python3的超级和理解 - > TypeError?

在理解中使用python3的超级似乎总是导致TypeError: super(type, obj): obj must be an instance or subtype of type(但使用python 2的超级确实按预期工作)

class A(object):
    def __repr__(self):
         return "hi!"  

class B(A):
    def __repr__(self):
         return "".join(super().__repr__() for i in range(2))  

repr(B())
#output: <repr(<__main__.B at 0x7f70cf36fcc0>) failed: TypeError: super(type, obj): obj must be an instance or subtype of type>

class C(A):
    def __repr__(self):
        s = ''
        for i in range(4):
            s += super().__repr__()
        return s     

repr(C())
#output: hi!hi!hi!hi!

class D(A):
    def __repr__(self):
        return "".join(super(D,self).__repr__() for i in range(4))

repr(D())
#output: …
Run Code Online (Sandbox Code Playgroud)

python list-comprehension super python-3.x

10
推荐指数
1
解决办法
502
查看次数

访问ES6超级属性

所以当我看到令人惊讶的事情时,我正在搞乱ES6课程:

class Animal {
    constructor(name) {
        this.name = name;
    }
    speak(sound) {
        console.log(sound);
    }
}

class Dog extends Animal {
    constructor(name, age) {
        super(name);
        this.age = age;
    }
    speak() {
        super.speak("Woof! I'm " + super.name + " and am " + this.age);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我创造了我的狗:

var mydog = new Dog("mydog",3);
mydog.speak();
Run Code Online (Sandbox Code Playgroud)

现在这个打印

Woof! I'm undefined and am 3
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,为什么super.name未定义?我期待它mydog在这种情况下.

javascript inheritance class super ecmascript-6

10
推荐指数
1
解决办法
5586
查看次数

Typescript:从扩展类调用超级方法会出现类型错误 - (中间值)不是函数

我正在名为 StructureWindowComponent 的组件中实现事件处理,并且还在 LeggerStructureWindowComponent 中对其进行了覆盖。

在基类(StructureWindowComponent)中,模糊事件的事件处理如下:

symbolCellLostFocus = (symbolField : MatInput, $index: number) =>{
    console.log("base class symbol cell lost focus");
    //implementation...
  }
Run Code Online (Sandbox Code Playgroud)

在派生类 LeggerStructureWindowComponent 中,我使用 super 调用此方法,如下所示...

symbolCellLostFocus = (symbolField : MatInput, $index: number) =>{
    console.log("derived class symbol lost focus");
    super.symbolCellLostFocus(symbolField, $index);
  }
Run Code Online (Sandbox Code Playgroud)

我在控制台中收到错误: ERROR TypeError: (intermediate value).symbolCellLostFocus is not a function

不知道这里出了什么问题..有人可以请建议吗?

javascript event-handling super typescript angular

10
推荐指数
1
解决办法
4495
查看次数