# 使用類別

## 範例類別

```
class CRectangle {  //定義矩形類別
	//資料成員
	int width;  //寬
	int height; //長

	//函數成員
	int area(){   //計算面積
		return width*height;
	}
	int perimeter(){  //計算周長
		return 2*(width+height);
	}
}
```

以上範例為一個矩形的類別。

類別就好像你統整出來的一個盒子，盒子裡面有各式各樣的東西（資料成員與函數成員）。\
而若要使用，就必須先拿出這個盒子才可以使用裡面的東西。

### 資料成員

讓我們先初次帶入這個觀念到程式碼中：

```
class CRectangle {  //定義矩形類別
	//資料成員
	int width;  //寬
	int height; //長
}
public class ch08_1 {

	public static void main(String[] args) {
		CRectangle book;   //宣告CRectangle類別的變數 book
		book= new CRectangle(); //建立物件

		book.width=10;  //給寬一個值
		book.height=5;  //給長一個值

		System.out.println("寬＝ "+book.width);  //印出結果
		System.out.println("長＝ "+book.height); //印出結果

	}
}
```

在上述範例的矩形類別只用了資料成員。

想要計算一本書的寬與長，\
在主程式的地方我們宣告一個變數名稱為book，而這個變數的資料型態就是這個類別。

```
CRectangle book;
```

我們宣告了這個名稱之後，要賦予它功能，\
這時候利用 new 建立新的物件，並且讓book 指向這個類別。

```
book= new CRectangle();
```

如此一來就成功的建立這個類別的物件(instance)。

> ```
> CRectangle book;
> book= new CRectangle();
> ```
>
> 也可以寫成一行
>
> ```
> CRectangle book=new CRectangle();
> ```

知道如何宣告類別變數以及建立物件後，\
接下來就是要存取物件的內容。

```
物件名稱 . 資料成員 ;
```

如程式碼中：

```
book.width=10;  //存取物件的寬，並給他一個值
book.height=5;  //存取物件的長，並給他一個值
```

### 函數成員

再來我們將類別中的函數成員也加入：\
以下完整的程式碼

```
class CRectangle {  //定義矩形類別
	//資料成員
	int width;  //寬
	int height; //長
	//函數成員
	int area(){   //計算面積
		return width*height;
		}
	int perimeter(){  //計算周長
		return 2*(width+height);
		}
}
public class ch08_1 {

	public static void main(String[] args) {
		CRectangle book;   //宣告CRectangle類別的變數 book
		book= new CRectangle(); //建立物件

		book.width=10;  //給寬一個值
		book.height=5;  //給長一個值

		System.out.println("寬＝ "+book.width);  //印出結果
		System.out.println("長＝ "+book.height); //印出結果

		System.out.println("面積＝ "+book.area()); //印出面積
		System.out.println("面積＝ "+book.perimeter()); //印出周長
	}
}
```

如何呼叫類別裡的函數：

```
物件 . 函數名稱(引數1,引數2,...)
```

在本次的範例函數中，並沒有相關的引數，所以括號中沒有東西。\
呼叫函數的方式與資料成員意思相同。

## 補充：如何建立多個物件

```
package ch08;
class CRectangle {
	int width;
	int height;
}
public class ch08_2 {

	public static void main(String[] args) {
		CRectangle book,paper;   //宣告CRectangle類別的變數 book與 paper
		book= new CRectangle(); //建立物件
		paper= new CRectangle(); //建立物件

		book.width=10;  //給物件book的寬一個值
		book.height=5;  //給物件book的長一個值

		paper.width=5;  //給物件paper的寬一個值
		paper.height=20; //給物件paper的長一個值

		System.out.println("寬＝ "+book.width);  //印出結果
		System.out.println("長＝ "+book.height); //印出結果

		System.out.println("寬＝ "+paper.width);  //印出結果
		System.out.println("長＝ "+paper.height); //印出結果
	}
}
```

要建立多個物件也相當的簡單；\
在此範例多了新增一個paper的物件，\
於宣告時多宣告一個paper的變數，\
並且一樣使用new 來建立物件。

存取的方式也是一樣的：物件名稱 . 資料成員


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java.4-x.tw/java-08/java-08-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
