# 建構元間的呼叫 - 透過this

在建構元也可以運用 this 達到建構元間的呼叫。

以下範例為建構元利用this的技術來相互呼叫：

```
class CCircle
{
   private String color;
   private double pi=3.14;
   private double radius;

   public CCircle()           //這是一個沒有引數建構元
   {
      this("Green",1.0);      //這行將會呼叫有兩個引數的建構元
      System.out.println("CCircle() 呼叫");
   }
   public CCircle(String str, double r)    //這是一個有兩個引數的建構元
   {
      System.out.println("CCircle(String,double) 呼叫");
      color=str;
      radius=r;
   }
   public void show()
   {
      System.out.println("color="+color+", Radius="+radius);
      System.out.println("area="+pi*radius*radius);
   }
}
public class class_constructor_3
{
   public static void main(String args[])
   {
      CCircle cir1=new CCircle();
      cir1.show();
   }
}
```

以下針對這程式解說一下：

主程式部分，直接呼叫沒有引數的建構元

```
CCircle cir1=new CCircle();
```

讓我們看到沒有引數的建構元

```
public CCircle()           //這是一個沒有引數建構元
   {
      this("Green",1.0);      //這行將會呼叫有兩個引數的建構元
      System.out.println("CCircle() 呼叫");
   }
```

讓我們再看到有兩個引數的建構元

```
public CCircle(String str, double r)    //這是一個有兩個引數的建構元
   {
      System.out.println("CCircle(String,double) 呼叫");
      color=str;
      radius=r;
   }
```

剛剛在沒有引數的建構元當中：

```
this("Green",1.0);
```

把color 設為Green，radius設為1.0；\
因為要呼叫下方 擁有兩個引數的建構元，\
因此以this() 來呼叫。

## 注意

this ( )  要寫在建構元的敘述第一行；以下情況是不行的：

```
public CCircle()           //這是一個沒有引數建構元
   {
      System.out.println("CCircle() 呼叫");
      this("Green",1.0);     //這行寫法是錯誤的
   }
```

正確寫法為：

```
public CCircle()           //這是一個沒有引數建構元
   {
      this("Green",1.0);      //這行將會呼叫有兩個引數的建構元
      System.out.println("CCircle() 呼叫");
   }
```


---

# 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-09/java-09-3.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.
