错误原因:Rectangle虽然继承了B类,但还有接口A,Rectangle重写的是接口里边的两个方法和继承B类的属性,而没重写的A里边的方法没有得到Rectangle继承B的值。所以一直运行为0。
修改方法有2种:
1>---需要给Rectangle重写一下,如下:
public Rectangle(double width, double length) {
super(width, length);
this.width=width;
this.length=length;
}
2>---只需要改动B和Rectangle类的内容,请看代码,内容改动大不便细说:
class B implements A{
double width;
double length;
public B(double width,double length){
this.width=width;
this.length=length;
}
double area(){
return width*length;
}
double girth(){
return (width+length)*2;
}
}
class Rectangle extends B{
private double width,length;
public Rectangle(double width, double length) {
super(width, length);
}
}
Rectangle c1=new Rectangle(12.8,14.5);
楼主的Rectangle 明明没有带参的构造函数这里怎么调用了带参的构造函数,请要重写带参的构造函数