1、创建一个矩形类Rectangle:

JavaScript018

1、创建一个矩形类Rectangle:,第1张

class Rectangle

{

private:

double Length

double Width

public:

Rectangle(){Length=1Width=1}

Rectangle(double len, double w){Length=lenWidth=w}

double Perimeter(){return 2*(Length+Width)}

double Area(){return (Length*Width)}

void show(char *buf){sprintf(buf,"%矩形长=%f,宽=%f", Length, Width)}

}//读写矩形的长宽的方法你就自己写吧,很简单的int main(){ Rectangle test1(4,7) Rectangle test1(2,8)}

// 矩形

public class RectangleDemo {

public static void main(String[] args) {

RectangleDemo demo = new RectangleDemo(12, 32)

System.out.println(demo.getPerimeter())

System.out.println(demo.getArea())

demo = new RectangleDemo()

System.out.println(demo.getArea())

System.out.println(demo.getPerimeter())

demo.setHeight(50)

demo.setWidth(30)

System.out.println(demo.getArea())

System.out.println(demo.getPerimeter())

}

// 求周

public double getPerimeter() {

return (height + width) * 2

}

// 求面积

public double getArea() {

return height * width

}

public RectangleDemo(double height, double width) {

this.height = height

this.width = width

}

public RectangleDemo() {

this.height = 10

this.width = 10

}

private double height// 高度

private double width// 宽度

public double getHeight() {

return height

}

public void setHeight(double height) {

this.height = height

}

public double getWidth() {

return width

}

public void setWidth(double width) {

this.width = width

}

}

编写矩形类RectangleJava程序矩形类两数据员别rLength宽rWidth通getLength()、getWidth()、getArea()别查看矩形、宽面积通setLength()setWidth()重新设置矩形宽

public class Rect {

private double length//矩形的长

private double width//矩形的宽

public Rect() {}//默认构造器

public Rect(double length,double width) {

this.length = length

this.width = width

}

/**

* 取得矩形的面积

* */

public double getArea(){

return this.getLength() * this.getWidth()

}

/**

* 取得矩形的周长

* */

public double getPerimeter(){

return (this.getLength() + this.getWidth()) * 2

}

/**

* 取得矩形的面积,需传入矩形长与宽

* */

public double getArea(double length,double width){

return length * width

}

/**

* 取得矩形的周长,需传入矩形长与宽

* */

public double getPerimeter(double length,double width){

return (length + width) * 2

}

public double getLength() {

return length

}

public void setLength(double length) {

this.length = length

}

public double getWidth() {

return width

}

public void setWidth(double width) {

this.width = width

}

}