302班

java突击队
posts - 151, comments - 74, trackbacks - 0, articles - 14
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Java编写的Rectangle类

Posted on 2007-04-07 22:04 停留的风 阅读(1512) 评论(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());
        }
}


只有注册用户登录后才能发表评论。


网站导航: