Posted on 2007-04-07 22:04
停留的風 閱讀(1521)
評論(0) 編輯 收藏
public class Rectangle
{
private double width;
private double height;
private String color="white";
//無參構造器
public Rectangle()
{}
//有參構造器
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
//屬性的get和set方法定義
public void setWidth(double width)
{
this.width = width;
}
public double getWidth()
{
return this.width;
}
public void setHeight(double height)
{
this.height = height;
}
public double getHeight()
{
return this.height;
}
public void setColor(String color)
{
this.color=color;
}
public String getColor()
{
return this.color;
}
//計算周長的方法
private double getPerimeter()
{
return (width+height)*2;
}
//計算面子的方法
private double getArea()
{
return width*height;
}
public static void main(String[] args)
{
Rectangle rec = new Rectangle(3.6,5.8);
System.out.println("The perimeter of Rectangle is:"+rec.getPerimeter());
System.out.println("The area of Rectangle is:"+rec.getArea());
System.out.println("The color of Rectangle is:"+rec.getColor());
}
}