日期:2014-05-20  浏览次数:20732 次

谁来帮帮我~~多谢了
谁能详细帮我分析一下这个程序呢?谢谢了!!!!!

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());
}
}


------解决方案--------------------
你说你哪里不懂???

建议楼主自己看一下基础一点的书!!
------解决方案--------------------
这个程序嘛,就算你不会JAVA,只要会C++甚至C,就应该看得懂。
------解决方案--------------------

//package test.oo.shape; //这个注意考包的结构


public class Rectangle extends MyShape //这个注意public
{

private double length;
private double width;
public static final String SIDEERR = "长方形的长和宽必须大于0! "; //这个注意考static 和final

public 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;
}