
? ?
java rectangle是什么?讓我們一起來了解一下吧!
java rectangle是一個(gè)“區(qū)域”類,它的最大作用就是定義一個(gè)矩形的區(qū)域。Rectangle 指定坐標(biāo)空間中的一個(gè)區(qū)域,通過坐標(biāo)空間中Rectangle對(duì)象左上方的點(diǎn) (x,y)、寬度和高度可以定義這個(gè)區(qū)域。

其構(gòu)造函數(shù)Rectangle(int x, int y, int width, int height)
height? Rectangle?的高度。? width? Rectangle?的寬度。? x Rectangle?左上角的?X?坐標(biāo)。? y? Rectangle?左上角的?Y?坐標(biāo)。
以下兩個(gè)是其比較常用的方法:
boolean?contains(int?x,int?y)-->判定點(diǎn)(x,y)是否包含在指定區(qū)域內(nèi) boolean?contains(int?x,int?y,int?width,int?height)-->判定指定區(qū)域是否在其指定區(qū)域內(nèi)
實(shí)戰(zhàn)操作,具體步驟如下:
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;
?}
?/**
??*?測(cè)試函數(shù)
??*?@param?args
??*/
?public?static?void?main(String[]?args)?{
??Rectangle?rec=new?Rectangle(3,?4,?"紅色");
??rec.getArea();
??System.out.println(rec.toString());
?}
}以上就是小編今天的分享了,希望可以幫助到大家。
