Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扩展Button类以创建单例_Java_Android_Button_Singleton - Fatal编程技术网

Java 扩展Button类以创建单例

Java 扩展Button类以创建单例,java,android,button,singleton,Java,Android,Button,Singleton,我想扩展Button类来创建一个名为LabeledButton的单例类,该类还有一个名为label的String属性 到目前为止,我得到了: static public class LabeledButton extends Button { String label; private LabeledButton(String label) { this.label = label; } static public LabeledButton

我想扩展Button类来创建一个名为LabeledButton的单例类,该类还有一个名为label的String属性

到目前为止,我得到了:

static public class LabeledButton extends Button {
    String label;

    private LabeledButton(String label) {
        this.label = label;
    }

    static public LabeledButton getInstance(String label) {
        if(current == null)
            current = new LabeledButton(label);
        return current;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }
}
但这就是我在构造函数中得到的错误:

“android.widget.Button”中没有可用的默认构造函数


我做错了什么?

就像错误说需要为添加默认构造函数一样,请尝试将构造函数更改为以下内容

public class LabeledButton extends Button {
    String label;

    public LabeledButton(Context context, String label) {
        super(context);
        this.label = label;
    }

    static public LabeledButton getInstance(Context context, String label) {
        if(current == null)
            current = new LabeledButton(context, label);
        return current;
    }
}

如错误所述,您需要为添加默认构造函数,请尝试将构造函数更改为以下内容

public class LabeledButton extends Button {
    String label;

    public LabeledButton(Context context, String label) {
        super(context);
        this.label = label;
    }

    static public LabeledButton getInstance(Context context, String label) {
        if(current == null)
            current = new LabeledButton(context, label);
        return current;
    }
}
默认构造函数是没有任何参数的构造函数,如果没有提到其他构造函数,Java编译器会隐式引入默认构造函数

无论何时扩展任何给定的类,在创建对象时,都会首先调用父类构造函数。在父类不提供无参数构造函数的情况下,您必须显式地告诉调用哪个参数化构造函数

如错误中所述

There is no default constructor available in 'android.widget.Button'
在当前上下文中,因为您正在扩展按钮,而该按钮恰好没有默认的无参数构造函数

Button(Context context)
Button(Context context, AttributeSet attrs)
Button(Context context, AttributeSet attrs, int defStyleAttr)
Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
是唯一暴露的。

因此,您必须明确地调用其中一个参数化构造函数

public class LabeledButton extends Button {
    String label;

    public LabeledButton(Context context, String label) {
        super(context);  // Explicitly Invoking the parent parameterized constructor 
        this.label = label;
    }
}
默认构造函数是没有任何参数的构造函数,如果没有提到其他构造函数,Java编译器会隐式引入默认构造函数

无论何时扩展任何给定的类,在创建对象时,都会首先调用父类构造函数。在父类不提供无参数构造函数的情况下,您必须显式地告诉调用哪个参数化构造函数

如错误中所述

There is no default constructor available in 'android.widget.Button'
在当前上下文中,因为您正在扩展按钮,而该按钮恰好没有默认的无参数构造函数

Button(Context context)
Button(Context context, AttributeSet attrs)
Button(Context context, AttributeSet attrs, int defStyleAttr)
Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
是唯一暴露的。

因此,您必须明确地调用其中一个参数化构造函数

public class LabeledButton extends Button {
    String label;

    public LabeledButton(Context context, String label) {
        super(context);  // Explicitly Invoking the parent parameterized constructor 
        this.label = label;
    }
}

谢谢你的回答,但是getInstance呢?我应该在参数中添加哪个上下文?更新为包含GetInstanceHanks以获得答案,但是getInstance呢?我应该在参数中添加哪个上下文?更新为包含getInstance