小编use*_*927的帖子

关于Java中的构造函数

我正在做研究,其中我发现了以下内容:

假设我有一个类似下面的类,具有以下构造函数:

public class Triangle implements Shape {

    public String  type;
    public String height;

    public Triangle(String type) {
        super();
        this.type = type;
    }

    public Triangle(String height) {
        super();
        this.height = height;
    }

    public Triangle(String type, String height) {
        super();
        this.type = type;
        this.height = height;
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个编译时错误.但是,如果我改变height来自Stringint一切工作正常.以下是更改的代码:

public class Triangle implements Shape {

    public String  type;
    public int height;

    public Triangle(String type) {
        super();
        this.type = type;
    }

    public Triangle(int height) {
        super(); …
Run Code Online (Sandbox Code Playgroud)

java constructor

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

标签 统计

constructor ×1

java ×1