繁体   English   中英

如何在一个Java构造函数中调用this()和super(sth)?

[英]How do I call this() and super(sth) in one java contructor?

我需要知道在Java中是否可以进行这种构造函数链调用吗? 我正在扩展基本JButton类,我需要首先初始化超级变量,然后使用默认构造函数初始化我的类。

public CustomButton(){
    try {
        URL inp = CustomButton.class.getResource("/icons/noa_en/buttonBackground.png");
        background = ImageIO.read(inp);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public CustomButton(ImageIcon img){
    super(img);
    this();
}

要么 :

public CustomButton(ImageIcon img){
    this();
    super(img);
}

您只能在构造函数中的第一个动作时调用另一个构造函数。 因此,当您要在类的两个构造函数中调用不同的超类构造函数时,必须将通用代码重构为一个单独的方法:

public CustomButton() {
    // implicitly calls super() here
    setup();
}
public CustomButton(ImageIcon img) {
    super(img);
    setup();
}
private void setup() {
    // your init code
}

你不能叫这个

public CustomButton(ImageIcon img){
    super(img);
    this();
}

因为super()否则this将是第一行

因此,您不能同时调用super()或this()

您始终可以使用实例初始化块来代替私有init()方法。
这样,您将不需要在所有构造函数中重复调用init() super()构造函数完成后,将为每个构造函数调用此块。 请参见下面的示例:

class Parent {
    Parent(String s) {
        System.out.println("parent constructor");
    }
}

class Child extends Parent {
    int x, y, z;
    {
        // do object initialization here
        // whatever you do in your setup() method you can do here
        // this block is executed before each constructor of Child class
        x = 1; y = 2; z = 3; // assign default values
        System.out.println("Child object initialization");
    }

    Child(int new_x) {
        super("parent");
        System.out.println("Child constructor");
        // do some specific initialization
        x = new_x;
    }

    public static void main(String... args) {
        Child c = new Child(3); // prints
        // parent constructor -> Child object initialization -> Child constructor
        System.out.println(c.x); // 3
    }
}

希望能有所帮助。

在构造函数中,只能调用super()this()构造函数。 如果您什么都不提供,那么将隐式调用super()

您只能从构造函数中调用一个构造函数,它应该是第一行。 您可以先调用this构造函数,然后再从被调用的构造函数中调用super构造函数。

检查一下:

class Parent {

    public Parent() {
        System.out.println("Parent: No args constructor called");
    }
}

public class Child extends Parent{
    String demo;

    public Child(String demo) {
        super();
        this.demo = demo;
        System.out.println("Child: Single arg constructor called");
    };

    public Child() {
        this("hello");
        System.out.println("Child: No arg constructor called");
    }

    public static void main(String args[]) {
        Child demo = new Child();
    }


}

输出:

父:没有调用args构造函数

子级:单个arg构造函数

称为Child:未调用arg构造函数

this()super()只能在构造函数的第一行中调用。
这意味着您可以调用this()super()因为其中只有一个可以作为第一行。

如果您不提及this()super()则编译器将调用super() (不带参数)。

我可以通过执行以下操作来解决您的问题:

private void init()
{
    try {
            URL inp = CustomButton.class.getResource("/icons/noa_en/buttonBackground.png");
            background = ImageIO.read(inp);
        } catch (IOException e) {
            e.printStackTrace();
    }
}

public CustomButton()
{
    init();
}

public CustomButton(ImageIcon img){
    super(img);
    init()
}

更新:

请注意您提供的代码:

public CustomButton(){
    super();    // This gets automatically added to the constructor
    try {
        URL inp = CustomButton.class.getResource("/icons/noa_en/buttonBackground.png");
        background = ImageIO.read(inp);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

注意CustomButton()super() CustomButton() 因此,在这种情况下,这意味着:

public CustomButton(ImageIcon img){
    super(img);
    this();
}

super()被调用两次,一次由CustomButton()和一次由CustomButton(ImageIcon img)调用,这可能导致意外结果,具体取决于JButton的构造函数

出于这个原因,Java希望this()super()占据第一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM