翠岭华庭产权:圆类构造方法java

来源:百度文库 编辑:中科新闻网 时间:2024/05/11 02:48:31
下面给出了一个矩形类Rectangle,其中含有多个构造方法。上机编译并运行此程序,观察运行结果,体会其中不同构造方法的设计和使用,特别是对象作为参数时的情况。(要求实验报告里写出结果并分析。)

class Rectangle
{
private int width;
private int length;
//下面是类Rectangle的三个构造方法
Rcectangle() //此构造方法无参数,缺省地给出长(30)和宽(20)
{
length=30;width=20;
}
Rectangle(int 1,int w) //此构造成方法给出长和宽
{
length=1;width=w;
}
Rectangle(Rectangle r) //此构造方法以另一个Rectangle作为参数
{
width=r.width();
length=r.length();
}
int width() //返回宽度
{
return width;
}
int length() //返回长度
{
return length;
}
}
public class CRctngle
{
public static void main (String args[ ])
{
Rectangle x1=new Rectangle();
Rectangle x2=new Rectangle(50,40);
Rectangle x3=new Rectangle(xl);
System.out.println(x1.length());
System.out.println(x1.width());
System.out.println(x2.length());
System.out.println(x2.width());
System.out.println(x3.length());
System.out.println(x3.width());
}
}

4.参照上面的程序,自己编写一个包含圆类的程序,并为圆类设计几个构造方法