我正在做研究,我发现了以下内容:
假设我有一个像下面这样的类,具有以下构造函数:
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;
}
}
这给了我一个编译时错误。但是,如果我height
从更改String
为int
一切正常。下面是修改后的代码:
public class Triangle implements Shape {
public String type;
public int height;
public Triangle(String type) {
super();
this.type = type;
}
public Triangle(int height) {
super();
this.height = height;
}
public Triangle(String type, int height) {
super();
this.type = type;
this.height = height;
}
}
现在的问题是:假设我想要和第一个案例String
一样height
;为什么失败了?请解释。