super

Java: Calling a super method which calls an overridden method

早过忘川 提交于 2019-11-27 03:57:42
问题 public class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public class SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass();

Constructor overriding

亡梦爱人 提交于 2019-11-27 03:45:06
问题 I have a class: class One def initialize; end end I need to create a new class with my own constructor like this: class Two < One def initialize(some) puts some super end end Two.new("thing") but when I launch the code, I got an error: thing test.rb:10:in `initialize': wrong number of arguments (1 for 0) (ArgumentError) 回答1: super in this case (without parentheses) is a special form. It calls the superclass method with the original params. Instead try calling super() 来源: https://stackoverflow

Is super() broken in Python-2.x? [closed]

坚强是说给别人听的谎言 提交于 2019-11-27 03:15:45
It's often stated that super should be avoided in Python 2. I've found in my use of super in Python 2 that it never acts the way I expect unless I provide all arguments such as the example: super(ThisClass, self).some_func(*args, **kwargs) It seems to me this defeats the purpose of using super() , it's neither more concise, or much better than TheBaseClass.some_func(self, *args, **kwargs) . For most purposes method resolution order is a distant fairy tale. Other than the fact that 2.7 is the last major release to Python 2, why does super remain broken in Python 2? How and why has Python 3's

Python super and setting parent class property

我怕爱的太早我们不能终老 提交于 2019-11-27 02:08:00
问题 I'm having a really strange problem with Python super() and inheritance and properties. First, the code: #!/usr/bin/env python3 import pyglet import pygame class Sprite(pyglet.sprite.Sprite): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.rect = pygame.Rect(0, 0, self.width, self.height) self.rect.center = self.x, self.y @property def x(self): return super().x @x.setter def x(self, value): super(Sprite, self.__class__).x.fset(self, value) self.rect.centerx = value

super() and @staticmethod interaction

我只是一个虾纸丫 提交于 2019-11-27 01:00:53
问题 Is super() not meant to be used with staticmethods? When I try something like 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 I get the following error 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

How does Python's “super” do the right thing?

﹥>﹥吖頭↗ 提交于 2019-11-26 21:30:15
I'm running Python 2.5, so this question may not apply to Python 3. When you make a diamond class hierarchy using multiple inheritance and create an object of the derived-most class, Python does the Right Thing (TM). It calls the constructor for the derived-most class, then its parent classes as listed from left to right, then the grandparent. I'm familiar with Python's MRO ; that's not my question. I'm curious how the object returned from super actually manages to communicate to calls of super in the parent classes the correct order. Consider this example code: #!/usr/bin/python class A

How to call super method from grandchild class?

∥☆過路亽.° 提交于 2019-11-26 20:47:42
问题 I am working with some code that has 3 levels of class inheritance. From the lowest level derived class, what is the syntax for calling a method 2 levels up the hierarchy, e.g. a super.super call? The "middle" class does not implement the method I need to call. 回答1: Well, this is one way of doing it: class Grandparent(object): def my_method(self): print "Grandparent" class Parent(Grandparent): def my_method(self): print "Parent" class Child(Parent): def my_method(self): print "Hello

super.onCreate(savedInstanceState);

∥☆過路亽.° 提交于 2019-11-26 19:19:48
I have created an Android Application Project and in MainActivity.java > onCreate() it is calling super.onCreate(savedInstanceState) . As a beginner, can anyone explain what is the purpose of the above line? Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls. Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity . In Java, when you inherit from a class, you can override its methods to run your own code in them. A very common example of this is the overriding of

How to avoid infinite recursion with super()?

我的梦境 提交于 2019-11-26 18:53:11
I have code like this: class A(object): def __init__(self): self.a = 1 class B(A): def __init__(self): self.b = 2 super(self.__class__, self).__init__() class C(B): def __init__(self): self.c = 3 super(self.__class__, self).__init__() Instantiating B works as expected but instantiating C recursed infinitely and causes a stack overflow. How can I solve this? When instantiating C calls B.__init__ , self.__class__ will still be C, so the super() call brings it back to B. When calling super(), use the class names directly. So in B, call super(B, self) , rather than super(self.__class__, self) (and

When calling super() in a derived class, can I pass in self.__class__? [duplicate]

陌路散爱 提交于 2019-11-26 18:34:30
This question already has an answer here: How to avoid infinite recursion with super()? 1 answer I've recently discovered (via StackOverflow) that to call a method in a base class I should call: super([[derived class]], self).[[base class method]]() That's fine, it works. However, I find myself often copying and pasting between classes when I make a change and frequently I forget to fix the derived class argument to the super() function. I'd like to avoid having to remember to change the derived class argument. Can I instead just use self.__class__ as the first argument of the super() function