标签: super

JAVA中的super()函数

按钮类:

class SubmitButton extends JButton implements ActionListener {
    public SubmitButton(String title){
        super(title);
        ....
Run Code Online (Sandbox Code Playgroud)

我在哪里声明:

SubmitButton submit = new SubmitButton("Submit");
submit.setBounds(530+150, 200, 100, 25);
Run Code Online (Sandbox Code Playgroud)

super(title)如何将String标题设置为按钮的标题?它如何与常规JButtons的方法.setText()相同?

换句话说,如何调用super()与.setText()做同样的事情super()知道如何更改标题?是.setText()是JButton Class中唯一一个以字符串作为参数的方法吗?

java super

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

你把super()调用作为构造函数的开头吗?

这是一个关于编码风格和推荐做法的问题:

正如在问题的答案中解释的那样,在构造函数中放置super()是不必要的?,如果你为一个应该使用超类中的默认(no-arg)构造函数的类编写构造函数,你可以super()在构造函数的开头调用:

public MyClass(int parm){
  super(); // leaving this out makes no difference
  // do stuff...
}
Run Code Online (Sandbox Code Playgroud)

但你也可以省略通话; 在两种情况下,编译器都会像super()调用一样.

那么,你是否将呼叫置于你的构造函数中?

一方面,人们可能会争辩说,包括super()使事情更明确.OTOH,我总是不喜欢编写冗余代码,所以我个人倾向于将其删除; 然而,我经常在其他人的代码中看到它.

你有什么经历?你有一种或另一种方法有问题吗?您是否有规定一种方法的编码指南?

BTW:相关问题(仅供参考):

当没有其他构造函数时,是否有理由显式编写默认构造函数?

java redundancy constructor coding-style super

7
推荐指数
1
解决办法
1476
查看次数

准备超级构造函数的参数

我有一个必须用参数构造的基类.在子类中,我需要在构造基类之前准备这个参数,但是在Java中必须先调用super.处理这种情况的最佳方法是什么(见下面的简单示例).

class BaseClass {
    protected String preparedParam;

    public BaseClass(String preparedParam) {
        this.param = param;
    }
}

class ChildClass {

    public ChildClass (Map<String, Object> params) {
        // need to work with params and prepare param for super constructor
        super(param);
    }
}
Run Code Online (Sandbox Code Playgroud)

java constructor super

7
推荐指数
2
解决办法
3808
查看次数

如何在Ruby中重新定义Fixnum的+(plus)方法并保留原始+功能?

这引发了我在1.9.2 Ruby中的SystemStackError(但在Rubinius中工作):

class Fixnum
  def +(other)
   self + other * 2
  end
end
Run Code Online (Sandbox Code Playgroud)

但没有super+(基于其他错误).

我如何访问原始+功能?

ruby rubinius yarv super

7
推荐指数
1
解决办法
5601
查看次数

使用__init__ for PyQt4的不同方法

所以......我正在尝试使用PyQt4从基本的Python转向一些GUI编程.我正在看几本不同的书籍和教程,他们每个人似乎都有一种稍微不同的方式来推动课程定义.

一个教程从这样的类开始:

class Example(QtGui.QDialog):
    def __init__(self):
        super(Example, self).__init__()
Run Code Online (Sandbox Code Playgroud)

另一本书是这样的:

class Example(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Example, self).__init__(parent)
Run Code Online (Sandbox Code Playgroud)

而另一个是这样做的:

class Example(QtGui.QDialog):
    def__init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
Run Code Online (Sandbox Code Playgroud)

我仍然在试图总结我的脑海围绕类和OOP和super()所有......我在想,在第三个例子中的最后一行完成或多或少同样的事情,使用呼叫纠正super()在前面的,通过明确直接调用基类?对于这些相对简单的例子,即单一继承,是否有任何真正的好处或理由使用单向与另一种方式?最后......第二个例子parent作为一个参数传递,super()而第一个例子没有...任何关于为什么/何时/哪里合适的猜测/解释?

python pyqt super pyqt4

7
推荐指数
1
解决办法
3630
查看次数

访问非重写的超类方法时使用'super'关键字

我试图在Java中获得继承权,并且已经了解到当在子类中重写方法(和隐藏字段)时,仍然可以使用"super"关键字从超类中访问它们.

我想知道的是,'super'关键字是否应该用于非重写方法?

有没有区别(对于非重写方法/非隐藏字段)?

我在下面举了一个例子.

public class Vehicle {
    private int tyreCost;

    public Vehicle(int tyreCost) {
         this.tyreCost = tyreCost;
    }

    public int getTyreCost() {
        return tyreCost;
    }        
}
Run Code Online (Sandbox Code Playgroud)

public class Car extends Vehicle {
    private int wheelCount;

    public Vehicle(int tyreCost, int wheelCount) {
        super(tyreCost);
        this.wheelCount = wheelCount;
    }   

    public int getTotalTyreReplacementCost() {
        return getTyreCost() * wheelCount;
    }   
}
Run Code Online (Sandbox Code Playgroud)

具体来说,既然getTyreCost()没有被覆盖,应该getTotalTyreReplacementCost()使用getTyreCost(),还是super.getTyreCost()

我想知道是否应该在访问超类的字段或方法的所有实例中使用super(在代码中显示您正在访问超类),或者仅在重写/隐藏的那些实例中使用super(因此它们脱颖而出).

java coding-style super

7
推荐指数
1
解决办法
6646
查看次数

对super()的调用必须是构造函数体中的第一个语句

我正在编写一个LoginRequest类的构造函数,它扩展了一个名为JsobObjectRequest的类(来自Android中的Volley框架,但这与问题完全无关)

使用此代码:

 public LoginRequest(String username, String password, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
        Boolean hasCredentials=(username!=null && password!=null);
        int method=hasCredentials? Method.POST:Request.Method.GET;
        super(method, API_URL_LOGIN, null, responseListener, errorListener);

        this.username=username;
        this.password=password;

    }
Run Code Online (Sandbox Code Playgroud)

我得到错误:调用super()必须是构造函数体中的第一个语句

相反,这段代码编译得很好:

 public LoginRequest(String username, String password, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
        super((username!=null && password!=null)? Method.POST:Request.Method.GET, API_URL_LOGIN, null, responseListener, errorListener);

        this.username=username;
        this.password=password;

    }
Run Code Online (Sandbox Code Playgroud)

但这不是有效的完全相同的事情吗?在这两种情况下,在调用超级构造函数之前,根据传递给子类构造函数的参数值,进行一些简单的计算.为什么编译器不能编译第一个例子,因为它可以编译第二个例子?

call-super-constructor-must-first-statement语句规范是否比它需要的更简单,或者我错过了什么?

编辑:这被错误地标记为重复为什么this()和super()必须是构造函数中的第一个语句?.这个问题更通用,并询问为什么super()必须是第一个声明.这里的问题是为什么像我发布的那样的案例会破坏这些要求(并且在这个问题中得到了令人满意的回答)

java constructor super

7
推荐指数
1
解决办法
8640
查看次数

Python中的super().__ init __()和显式超类__init __()之间的行为差​​异

super().__init__()在代码中使用和显式调用超类构造函数之间的行为有一个无法解释的差异.

class IPElement(object):

def __init__(self, ip_type='IPv4'):
    self.ip_type = ip_type

class IPAddressSimple(IPElement):

    def __init__(self, ip_name, ip_type='IPv4'):
        self.ip_name = ip_name
        super().__init__(self, ip_type=ip_type)
Run Code Online (Sandbox Code Playgroud)

这里,该行super().__init__(self, ip_type=ip_type)导致类型错误:

TypeError: __init__() got multiple values for argument 'ip_type'
Run Code Online (Sandbox Code Playgroud)

当我将调用更改为ip_type按位置传递时(例如,super().__init__(self, ip_type)我得到一个不同的类型错误:

TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
Run Code Online (Sandbox Code Playgroud)

这些错误都没有对我有意义,当我用super()超类的显式名称替换时,一切都按预期工作.以下工作正常,就像按位置传递ip_type一样:

class IPAddressSimple(IPElement):

        def __init__(self, ip_name, ip_type='IPv4'):
            self.ip_name = ip_name
            IPElement.__init__(self, ip_type=ip_type)
Run Code Online (Sandbox Code Playgroud)

__init__如果有必要,我当然可以使用显式调用超类方法,但我想理解为什么super()不在这里工作.

inheritance super keyword-argument python-3.x

7
推荐指数
1
解决办法
517
查看次数

如果在构造函数中使用super调用重写方法会发生什么

有两个类Super1Sub1

Super1.class

public class Super1 {
    Super1 (){
        this.printThree();
    }

    public void printThree(){
        System.out.println("Print Three");
    }    
}
Run Code Online (Sandbox Code Playgroud)

Sub1.class

public class Sub1 extends Super1 {
    Sub1 (){
        super.printThree();
    }

    int three=(int) Math.PI;

    public void printThree(){
        System.out.println(three);
    }

    public static void main(String ...a){
         new Sub1().printThree();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我调用printThree类的方法时,Sub1我希望输出为:

打印三
3

因为Sub1构造函数调用了super.printThree();.

但我真的得到了

0
打印3
3

我知道0是默认值,int但它是如何发生的?

java constructor super

7
推荐指数
1
解决办法
651
查看次数

"对成员'init(...)的模糊引用"调用baseclass初始化程序

我有一个基类:

class ViewController: UIViewController
{
    init(nibName nibNameOrNil: String?)
    {
        super.init(nibName: nibNameOrNil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) { }
}
Run Code Online (Sandbox Code Playgroud)

子类:

class OneViewController: ViewController
{
    private var one: One
    init(one: One)
    {
        self.one = one

        super.init(nibName: "OneNib")
    }

    required init?(coder aDecoder: NSCoder) { }
}
Run Code Online (Sandbox Code Playgroud)

以及上述子类的子类:

class TwoViewController: OneViewController
{
    private var two: Two
    init(two: Two)
    {
        self.two = two

        super.init(nibName: "TwoNib") <== ERROR
    }

    required init?(coder aDecoder: NSCoder) { }
}
Run Code Online (Sandbox Code Playgroud)

在指定的行我得到错误:

Ambiguous reference to member 'init(one:)'

我不明白为什么编译器无法弄清楚我指的是 …

inheritance super ios swift swift2

7
推荐指数
1
解决办法
4749
查看次数