房天下 > 房天下问答 > 业主生活 > 其他
  • java程序出错,不知什么原因。。

    class Rect { private float length; //矩形的长度,float类型 private float width; //矩形的宽度,float类型 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(){ return "矩形长度: "+this.length+"\n"+"矩形宽度: "+this.width+"\n" ; } //输出矩形信息 public float getPerimeter(){ return 2*(length+width); } //得到矩形的周长 public float getArea(){ return length*width; } //得到矩形的面积 } class Recter extends Rect{ private float height; //长方体的高度,float类型 public Recter (float l, float w,float h){ super(l,w); height = h; } //带参数的构造方法 //需要调用基类带参数构造方法 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; } //重写toString()方法 } public class RectTest { public static void main(String args[]){ Rect ry = new Rect(10.5,6.5,5.5); System.out.println(ry.toString()); } } 运行时提示:找不到符号....... 而且也不能用Recter 创建对象,一创建,还是提示:找不到符号..... 如果是这样的话:Rect ry = new Rect();就没提示错误。。。但运行结果是:矩形长度:0.0 矩形宽度:0.0 不对。。要的是定义一个主类:RectTest,在它的main()方法中创建一个矩形长度为10.5,矩形宽度为:5.5, 长方体的高度为:6.5的长方体对象ry,然后调用toString()方法输出其相应信息。

    提问者:困学

    发布于2011-01-18

共1个回答
  • uclg_mhcy 丨Lv 0
    第一:Rect这个类里面根本找不到 Rect(float l,float w,float h)这个构造方法。 第二:Recter ry = new Recter(10.5,6.5,5.5);这样去构造一个对象,参数里的数字会当成double类型的去处理。要在数字后面加以个F,表示传入的参数是float类型。 Recter ry = new Recter(10.5F,6.5F,5.5F); 第三:Rect ry = new Rect();是用的默认构造函数去创建对象,没有传入参数,所以java虚拟机会默认给类的属性默认一个值。对于float类型就是0.0 第四:按照第二条中的讲的那样做,问题就可以解决了。
    +1 2011-01-18 举报
热门人气推荐
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。