A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shijialong1111 中级黑马   /  2016-7-3 10:06  /  933 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

需求:
                * 定义一个长方形类,定义 求周长和面积的方法,
                * 然后定义一个测试类进行测试。

1 个回复

倒序浏览
  1. public class Rectangle {

  2.         // Rectangle : 矩形
  3.         private double length = 0.0; // 长 属性
  4.         private double width = 0.0; // 宽 属性

  5.         public double getLength() {
  6.                 return length;
  7.         }

  8.         public void setLength(double length) {
  9.                 this.length = length;
  10.         }

  11.         public double getWidth() {
  12.                 return width;
  13.         }

  14.         public void setWidth(double width) {
  15.                 this.width = width;
  16.         }

  17.         // 自定义 计算周长方法
  18.         public double circumference() {

  19.                 return (this.length + this.width) * 2;
  20.         }

  21.         // 自定义 计算面积方法
  22.         public double area() {

  23.                 return this.length * this.width;
  24.         }

  25.         // 入口方法
  26.         public static void main(String[] args) {

  27.                 Rectangle rect = new Rectangle();

  28.                 rect.setLength(3.0);
  29.                 rect.setWidth(5.0);
  30.                 System.out.println("周长为:" + rect.circumference());
  31.                 System.out.println("面积为:" + rect.area());
  32.         }
  33. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马