谁能详细帮我分析一下这个程序呢?谢谢了!!!!!package test.oo.shape;
public class Rectangle extends MyShape 
{ private double length;
private double width;
public static final String SIDEERR = "长方形的长和宽必须大于0!"; public Rectangle()
{
init();
} public Rectangle(double a, double b)
{
if ((a <= 0) || (b <= 0))
{
System.err.println(SIDEERR);
init();

else 
{
this.length = a;
this.width = b;
}
} private void init()
{
this.length = 5;
this.width = 4;
}

public double getGirth() 
{
return (this.length + this.width) * 2;
}
public double getArea() 
{
return this.length * this.width;
}
public String toString()
{
return "矩形的名字是:" + this.name + ",长为" + this.length + ",宽为" + this.width;
}
public double getLength() 
{
return length;
}
public void setLength(double length) 
{
if (length > 0)
{
this.length = length;

else 
{
System.err.println(SIDEERR);
}
}
public double getWidth() 
{
return width;
}
public void setWidth(double width) 
{
if (width > 0) 
{
this.width = width;

else 
{
System.err.println(SIDEERR);
}
}
public static void main(String[] args) 
{
Rectangle test = new Rectangle();
test.setName("myRectangle");
System.out.println( test.toString());
System.out.println("矩形的周长是:" + test.getGirth());
System.out.println("矩形的面积是:" + test.getArea());
}
}

解决方案 »

  1.   

    其实是这样的,我是无奈要读这门课,我根本就是完全一点也不了解java,这是这次期末的考题,老师只给了程序,但是没有告诉怎么出题= =说是我们读懂了就一定会做的,我很苦恼啊!
    谁能帮帮我呢~~万分感谢啦
      

  2.   

    这个程序嘛,就算你不会JAVA,只要会C++甚至C,就应该看得懂。
      

  3.   


    //package test.oo.shape; //这个注意考包的结构
    public class Rectangle extends MyShape //这个注意public
    {private double length;
    private double width;
    public static final String SIDEERR = "长方形的长和宽必须大于0!"; //这个注意考static 和finalpublic Rectangle()     //注意考构造函数的格式
    {
    init();
    }public Rectangle(double a, double b)
    {
    if ((a <= 0) || (b <= 0))
    {
    System.err.println(SIDEERR) ;   //这儿注意考错误流
    init();

    else                                //这儿有个if else注意考这个语句
    {
    this.length = a;
    this.width = b;
    }
    }private void init()
    {
    this.length = 5;
    this.width = 4;
    }public double getGirth() 
    {
    return (this.length + this.width) * 2;
    }
    public double getArea() 
    {
    return this.length * this.width;
    }
    public String toString()          //这些方法或者说函数,注意考他的格式
    {
    return "矩形的名字是:" + this.name + ",长为" + this.length + ",宽为" + this.width;   //这边一个name没有声明,不知道是不是你考错了
    }
    public double getLength() 
    {
    return length;
    }
    public void setLength(double length) 
    {
    if (length > 0)
    {
    this.length = length;

    else 
    {
    System.err.println(SIDEERR);
    }
    }
    public double getWidth() 
    {
    return width;
    }
    public void setWidth(double width) 
    {
    if (width > 0) 
    {
    this.width = width;

    else 
    {
    System.err.println(SIDEERR);
    }
    }
    public static void main(String[] args)    //这儿注意main函数的格式 
    {
    Rectangle test = new Rectangle();
    test.setName("myRectangle");            //这儿还有一个setName函数没有声明
    System.out.println( test.toString());
    System.out.println("矩形的周长是:" + test.getGirth());
    System.out.println("矩形的面积是:" + test.getArea());
    }
    }//这题目真的不难,相信你一定能过的
      

  4.   

    汗,我没有冒充啊...我学的是工商管理,基本可以算文科了吧,大一的时候有过计算机课,不过从没接触过编程啊= =再说女生学这些本来就比较困难吧= =虽然我们java老师也是个女的
      

  5.   

    java 是个不错的语言,但是学起来比较抽象,真是很麻烦的。没事
    多做几个程序,就可以 了
      

  6.   

    package test;public class Rectangle extends MyShape {//继承MyShape类 private double length;//声明个浮点类型变量 private double width;//声明个浮点类型变量 public static final String SIDEERR = "长方形的长和宽必须大于0!";//声明个字符串类型常量 //程序的构造方法,也是入口
    public Rectangle() {
    init();//调用init();方法
    } //长度和宽度判断方法,判断是否小于0,需要提供参数两个且都是浮点类型
    public Rectangle(double a, double b) {
    if ((a <= 0) || (b <= 0)) {//判断是否小于0,小于0则进入该方法体
    System.err.println(SIDEERR);//输出前面声明的常量
    init();//调用init();方法
    } else {//如果不小于0,进入该方法体
    this.length = a;//给length赋值a
    this.width = b;//给width赋b
    }
    } private void init() {
    this.length = 5;//给length赋值5
    this.width = 4;//给width赋4
    } //计算周长的方法
    public double getGirth() {
    return (this.length + this.width) * 2;
    } //计算面积的方法
    public double getArea() {
    return this.length * this.width;
    } public String toString() {
    //this.name是调用父类里的成员,this.length是调用本类里的成员,this.width是调用本类里的成员
    return "矩形的名字是:" + this.name + ",长为" + this.length + ",宽为" + this.width;
    } public double getLength() {
    return length;
    } public void setLength(double length) {
    if (length > 0) {
    this.length = length;
    } else {
    System.err.println(SIDEERR);
    }
    } public double getWidth() {
    return width;
    } public void setWidth(double width) {
    if (width > 0) {
    this.width = width;
    } else {
    System.err.println(SIDEERR);
    }
    } public static void main(String[] args) {
    Rectangle test = new Rectangle();
    test.setName("myRectangle");
    System.out.println(test.toString());
    System.out.println("矩形的周长是:" + test.getGirth());
    System.out.println("矩形的面积是:" + test.getArea());
    }
    }
      

  7.   

    你应该是还有个父类代码如下
    public class MyShape {
    String name; public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    }