302班

          java突擊隊
          posts - 151, comments - 74, trackbacks - 0, articles - 14
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java編寫的Rectangle類

          Posted on 2007-04-07 22:04 停留的風 閱讀(1522) 評論(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());
                  }
          }


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 分宜县| 会东县| 铜陵市| 雷山县| 翁牛特旗| 双流县| 新龙县| 南宁市| 大足县| 新干县| 太原市| 石阡县| 西平县| 大宁县| 察雅县| 辽宁省| 县级市| 景宁| 大关县| 闽清县| 印江| 勃利县| 潼关县| 梨树县| 汶川县| 拜城县| 都兰县| 岳普湖县| 花莲市| 洛阳市| 博白县| 天等县| 墨竹工卡县| 勃利县| 石楼县| 洛浦县| 合山市| 呼玛县| 富平县| 临武县| 新化县|