? ?
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()); ?} }
以上就是小編今天的分享了,希望可以幫助到大家。