Java 程序一题出错!(关于矩形)

来源:百度知道 编辑:UC知道 时间:2024/05/29 02:56:12
class Rect {
private float length;
private float width;
public Rect(){
}
public Rect(float l, float w){
length = l;
width = w;
}
public void setLength(float l){
length = l;
}

public float getLength(){
return length;
}
public void setWidth(float w){
width = w;
}

public float getWidth(){
return width;
}
public String toString(){
String information="";
information= "矩形长度: "+length+"\n";
information= "矩形宽度: "+width+"\n";
return information;
}

问题2:
public class RectTest {
public static void main(String args[]){
Rect ry = new Rect();
System.out.println(ry.toString());

}
改为:
class RectTest {
public static void main(String args[]){
Rect ry = new Rect();
System.out.println(ry.toString());

}

问题1:
public float getVolume() {
return getLength() * getWidth() * height;
}

public String toString() {
String information = super.toString();
information = "长方形的高度: " + height + "\n";
information = "矩形底面积: " + getLength() * getWidth() + "\n";
information = "长方形体积: " + getLength() * getWidth() * height + "\n";
return information;
}

貌似private修饰的成员对子类是不可见的。

既然你把 length、width 设为 private 但是它们是属于 Rect这个类 当你用 Recter 继承Rect时 私有的属性是不会被继承的 所有在Recter中 直接用 length和width 就跟没有定义一样
我看你 用了 super