java rectangle

<link rel="stylesheet" href="https://js.how234.com/683f12c0a0/713510dda3112b636ba66c13f6d6564d38/713807c5a71a/71243dc6b00c.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/683f12c0a0/713510dda3112b636ba66c13f6d6564d38/713807c5a71a/71242ac1a704264e69a8610ffdca.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><style>pre{overflow-x: auto}</style>

   <link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

java rectangle是什么?让我们一起来了解一下吧!

java rectangle是一个“区域”类,它的最大作用就是定义一个矩形的区域。Rectangle 指定坐标空间中的一个区域,通过坐标空间中Rectangle对象左上方的点 (x,y)、宽度和高度可以定义这个区域。

java rectangle

其构造函数Rectangle(int x, int y, int width, int height)

height Rectangle 的高度。 width Rectangle 的宽度。 xRectangle 左上角的 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()); }}