标签: super

在Python中使用super()关键字的单继承的基本示例是什么?

假设我已经设置了以下类:

class Foo:
     def __init__(self, frob, frotz):
          self.frobnicate = frob
          self.frotz = frotz
class Bar:
     def __init__(self, frob, frizzle):
          self.frobnicate = frob
          self.frotz = 34
          self.frazzle = frizzle
Run Code Online (Sandbox Code Playgroud)

我怎么能(如果我可以)在这个上下文中使用super()来消除重复的代码?

python inheritance constructor super

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

构造函数重写

我有一节课:

class One
  def initialize; end
end
Run Code Online (Sandbox Code Playgroud)

我需要使用我自己的构造函数创建一个新类,如下所示:

class Two < One
  def initialize(some)
    puts some
    super
  end
end

Two.new("thing")
Run Code Online (Sandbox Code Playgroud)

但是当我启动代码时,我收到了一个错误:

thing
test.rb:10:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)
Run Code Online (Sandbox Code Playgroud)

ruby super

26
推荐指数
1
解决办法
7812
查看次数

构造函数中的super()

我正在阅读一些代码.在构造函数中它有super()但是类实现了接口,当然它没有构造函数.那么它指的是哪个super()?

public class BoundingBox implements IBoundingVolume {

public BoundingBox() {
        super();
        mTransformedMin = new Number3D();
        mTransformedMax = new Number3D();
        mTmpMin = new Number3D();
        mTmpMax = new Number3D();
        mPoints = new Number3D[8];
        mTmp = new Number3D[8];
        mMin = new Number3D();
        mMax = new Number3D();
        for(int i=0; i<8; ++i) {
            mPoints[i] = new Number3D();
            mTmp[i] = new Number3D();
        }
}


public interface IBoundingVolume {
    public void calculateBounds(Geometry3D geometry);
    public void drawBoundingVolume(Camera camera, float[] projMatrix, float[] vMatrix, float[] mMatrix);
    public void transform(float[] matrix); …
Run Code Online (Sandbox Code Playgroud)

java constructor super

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

Java中的超级关键字,有趣的行为,请解释一下

可以说我们有以下代码:

class A {

    public void doLogic() {
        System.out.println("doLogic from A");
    }
}

class B extends A {

    @Override
    public void doLogic() {
        System.out.println("doLogic from B");
    }

    public void doDifferentLogic() {
        System.out.println("doDifferentLogic from B");
        super.doLogic();
    }
}

class C extends B {

    @Override
    public void doLogic() {
        System.out.println("doLogic from C");
    }
}

public class Test {

    public static void main(String[] args) {
        C c = new C();
        c.doDifferentLogic();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我们执行这段代码预期的行为是:由于C拥有一个参考对象C类的,当你调用c.doDifferentLogic()的JVM搜索在C类中的方法方法,因为它没有发现它开始盯着继承树.正如预期的那样,该doDifferentLogic()方法可以在超类中找到并执行.然而,构造super.doLogic()期望从当前引用的"Point of View"看起来是C类型的.因此C的超级应该是B,而是调用来自顶级A的方法.

如果删除 …

java inheritance super

25
推荐指数
1
解决办法
4244
查看次数

当父不从对象继承时,Python 2.x super __init__继承不起作用

我有以下Python 2.7代码:

class Frame:
    def __init__(self, image):
        self.image = image

class Eye(Frame):
    def __init__(self, image):
        super(Eye, self).__init__()
        self.some_other_defined_stuff()
Run Code Online (Sandbox Code Playgroud)

我正在尝试扩展该__init__()方法,以便当我实例化一个'Eye'时,除了Frame设置之外,还会做一堆其他的东西(self.some_other_defined_stuff()).Frame.__init__()需要先跑.

我收到以下错误:

super(Eye, self).__init__()
TypeError: must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

其中我不明白其逻辑原因.有人可以解释一下吗?我习惯于在红宝石中输入'super'.

python inheritance super new-style-class python-2.7

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

在Objective C类别中使用Super?

我想覆盖Objective C类中我没有源代码的方法.

我已经研究过了,看来Categories应该允许我这样做,但是我想在我的新方法中使用旧方法的结果,使用super来获得旧方法的结果.

每当我尝试这个时,我的方法被调用,但"超级"是零...任何想法为什么?我正在使用XCode 2.2 SDK进行iPhone开发.我肯定在使用类的实例,类的方法是实例方法.

@implementation SampleClass (filePathResolver)
-(NSString*) fullPathFromRelativePath:(NSString*) relPath
{
    NSString *result = [super fullPathFromRelativePath: relPath];

  ... do some stuff with the old result

    return result;
}
Run Code Online (Sandbox Code Playgroud)

注意和澄清:从我在Apple Docs中看到的情况来看,在我看来应该允许这样做?

developer.apple.com上的类别文档: 当类别覆盖继承的方法时,类别中的方法可以像往常一样通过消息调用继承的实现到超级.但是,如果某个类别覆盖了类别类中已存在的方法,则无法调用原始实现.

objective-c super categories

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

Java如何调用盛大父母的方法?

可能重复:
为什么是super.super.method(); Java不允许?

假设我有3个类A,B并且C每个类都扩展了前一个类.

如何调用代码A.myMethod()C.myMethod()如果B也实现myMethod

class A
{
  public void myMethod()
  {
    // some stuff for A
  }
}

class B extends A
{
  public void myMethod()
  {
    // some stuff for B
    //and than calling A stuff
    super.myMethod();
  }
}

class C extends B
{
  public void myMethod()
  {
    // some stuff for C
    // i don't need stuff from b, but i need call …
Run Code Online (Sandbox Code Playgroud)

java overriding super

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

在Java中_not_调用超类构造函数的任何方法?

如果我有课:

class A {
   public A() { }
}
Run Code Online (Sandbox Code Playgroud)

和另一个

class B extends A {
   public B() { }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法B.B() 打电话A.A()

java oop constructor super

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

强制基本方法调用

Java或C#中是否有强制继承类来调用基础实现的构造?你可以调用super()或base()但是如果没有调用它可能会抛出编译时错误吗?那会很方便..

- 编辑 -

我主要是对重写方法感到好奇.

c# java polymorphism inheritance super

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

在javascript中模拟超级

基本上有一个很好的优雅机制来模拟super语法,就像下面的一样简单

  • this.$super.prop()
  • this.$super.prop.apply(this, arguments);

坚持的标准是:

  1. this.$super必须是原型的参考.即如果我在运行时更改超级原型,将反映此更改.这基本上意味着父母有一个新的属性然后这应该在所有孩子的运行时显示,super就像对父母的硬编码引用将反映更改
  2. this.$super.f.apply(this, arguments);必须适用于递归调用.对于任何链接的继承集,其中在继承链上进行多次超级调用时,您不能遇到递归问题.
  3. 您不得硬编码对孩子中超级对象的引用.即Base.prototype.f.apply(this, arguments);告诫这一点.
  4. 您不得使用X to JavaScript编译器或JavaScript预处理器.
  5. 必须符合ES5标准

天真的实现将是这样的.

var injectSuper = function (parent, child) {
  child.prototype.$super = parent.prototype;
};
Run Code Online (Sandbox Code Playgroud)

但这打破了条件2.

我见过的最优雅的机制是IvoWetzel的evalhack,它几乎是一个JavaScript预处理器,因此没有标准4.

javascript oop super

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