# 類別中使用 this

加上this 的用途是強調，於程式碼當中可以這麼做：

```
this . 資料成員名稱 ;
this . 函數成員名稱 ;
```

使用[上一篇](/java-08/java-08-3.md)修改後的範例作為this 的示範：

```
class CRectangle {  //定義矩形類別
	//資料成員
	int width;  //寬
	int height; //長
	//函數成員
	int area(){   //計算面積
		System.out.println("面積＝ "+this.width*this.height);
		}
	int perimeter(){  //計算周長
		System.out.println("周長＝ "+2*(this.width+this.height);
		}
        int show_all(){  //在show_all 函數中 呼叫計算面積的函數與計算周長的函數
                System.out.println("寬＝ "+this.width+" ,長＝ "+this.height);
                this.area();  //呼叫計算面積的函數
                this.perimeter(); //呼叫計算周長的函數
                }

}
public class ch08_4 {

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

		book.width=10;  //給寬一個值
		book.height=5;  //給長一個值
		book.show_all(); //呼叫show_all()
	}
}
```

this 代表 book

那麼一定會疑惑，那除了強調之外，沒有其他用途嗎？

在這邊舉另外一個簡單例子：

```
name(String book){
book=book;
}
```

如果在程式中，屬性的值與傳入的值都相同的話；兩者都會被認為是參數，所以book的值並不會被改變。

若改為以下則可以正常運作了：

```
name(String book){
this.book=book;
}
```


---

# 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-4.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.
