面向对象

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

~~

Rectangle r1 = new Rectangle(2, 3);

System.out.println(“周长是:” + r1.getLength() + "  " + “面积是:” + r1.getArea());

r1.setWidth(5);

r1.setHeight(6);

System.out.println(“周长是:” + r1.getLength() + "  " + “面积是:” + r1.getArea());

}

}

class Rectangle {

private int width;

private int height;

public Rectangle() {

}

public Rectangle(int width, int height) {

this.width = width;

this.height = height;

}

public void setWidth(int width) {

this.width = width;

}

public void setHeight(int height) {

this.height = height;

}

public int getLength() {

return (width + height) * 2;

}

public int getArea() {

return width * height;

}

1234567891011121314151617

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (1.编写一个计算各种形状的面积和周长的程序。 要求:父类Shape2D为一个抽象类,其中包含计算周长和计算面积两...
    盼旺阅读 2,017评论 0 1
  • C++ 面向对象 c++创建对象的时候如果使用new运算符,将会返回返回一个指针,指向堆中的内存地址 类,对象类定...
    小小小8021阅读 313评论 0 0
  • -什么是多态 -1.即不修改程序代码就可以改变程序运行时所绑定的具体代码,让程序可以选择多个运行状态,这就是多态性...
    黄敬理阅读 280评论 0 2
  • 尔本
    陈怂怂啊阅读 135评论 0 1
  • 序 《山娃的蜕变》,讲述了1998年,在私营经济蓬勃发展的温州,18岁的文白,一位从没出...
    阊江水阅读 281评论 2 4