构造函数难道可以有返回值类型?
在一本java书中见到这样一段代码://packagejava.learn;interfaceArea{publicabstractdoublearea();}publi...
在一本java书中见到这样一段代码:
//package java.learn;
interface Area {
public abstract double area();
}
public class Rectangle implements Area {
protected double length;
protected double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public Rectangle(double width) {
this(width, width);
}
public Rectangle() {
this(0, 0);
}
public double area() {
return this.width * this.length;
}
public double perimeter() {
return (this.width + this.length) * 2;
}
public String toString() {
return "一个矩形,长度" + length + ",宽度" + width + ",面积" + area() + ",周长:"
+ perimeter();
}
public static void main(String args[]) {
System.out.println(new Rectangle(10, 20).toString());
}
}
请大家注意,这里的构造函数都有public返回类型。怎么解释? 展开
//package java.learn;
interface Area {
public abstract double area();
}
public class Rectangle implements Area {
protected double length;
protected double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public Rectangle(double width) {
this(width, width);
}
public Rectangle() {
this(0, 0);
}
public double area() {
return this.width * this.length;
}
public double perimeter() {
return (this.width + this.length) * 2;
}
public String toString() {
return "一个矩形,长度" + length + ",宽度" + width + ",面积" + area() + ",周长:"
+ perimeter();
}
public static void main(String args[]) {
System.out.println(new Rectangle(10, 20).toString());
}
}
请大家注意,这里的构造函数都有public返回类型。怎么解释? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询