IT科技

當前位置 /首頁/IT科技 > /列表

rectangle,java

<link rel="stylesheet" href="https://js.how234.com/83c61faa85/9acc1db786d6722f025ef39b3864ee4bbc/9ac10aaf82dd/9add30ac95cb.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/83c61faa85/9acc1db786d6722f025ef39b3864ee4bbc/9ac10aaf82dd/9add27ab82c37f020050fe873378.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()); }}

TAG標籤:rectangle java #