
java rectangle是什么?让我们一起来了解一下吧!
java rectangle是一个“区域”类,它的最大作用就是定义一个矩形的区域。Rectangle 指定坐标空间中的一个区域,通过坐标空间中Rectangle对象左上方的点 (x,y)、宽度和高度可以定义这个区域。

其构造函数Rectangle(int x, int y, int width, int height)
height Rectangle 的高度。 width Rectangle 的宽度。 x Rectangle 左上角的 X 坐标。 y Rectangle 左上角的 Y 坐标。
以下两个是其比较常用的方法:
boolean contains(int x,int y)-->判定点(x,y)是否包含在指定区域内 boolean contains(int x,int y,int width,int height)-->判定指定区域是否在其指定区域内
实战操作,具体步骤如下:
public class Rectangle {
private double height;
private double width;
private String color;
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;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Rectangle(double width,double height,String color){
this.setColor(color);
this.setHeight(height);
this.setWidth(width);
}
public void getArea(){
double area=0;
area=this.height*this.width;
System.out.println("矩形的面积为"+area);
}
public String toString(){
String recStr="矩形的高度:"+this.getHeight()+"宽度:"+this.getWidth()
+"颜色:"+this.getColor();
return recStr;
}
/**
* 测试函数
* @param args
*/
public static void main(String[] args) {
Rectangle rec=new Rectangle(3, 4, "红色");
rec.getArea();
System.out.println(rec.toString());
}
}以上就是小编今天的分享了,希望可以帮助到大家。
