# 範例：介面

## 複習

在進入範例之前，再複習一次介面的重點：

* 介面中的資料成員必須設定初值
* 介面沒有一般函數，只有抽象函數

沿用一直以來喜歡使用的例子，CShape圖形、 CCircle圓形、 CRectangle矩形：\
套到本次範例 以類別CCircle圓形、 CRectangle矩形 實作CShape圖形介面

## 範例

### 定義介面

```
interface CShape               // 定義介面 CShape
{
   final double PI=3.14;        //final 可省略
   public abstract void show(); //public abstract 可省略
}
```

在剛剛定義了 CShape 介面，現在示範 CCircle 圓形類別 ：

```
class CCircle implementation CShape
{
   double radius;
   public CCircle(double r)   //CCircle建構元
   {
     radius=r;
   }
   public void show()         //改寫介面中抽象函數
   {
     System.out.println("面積＝"+PI*radius*radius);
   }
}
```

同理，撰寫 CRectangle 矩形類別

```
class CRectangle implements CShape
{
   int width,height;
   public CRectangle(int w,int h)  //CRectangle建構元
   {
      width=w;
      height=h;
   }
   public void show()
   {
      System.out.println("area="+width*height);
   }
}
```

### 主程式

到目前，我們已經學會如何定義介面，並以類別實作介面；接下來撰寫 main()

```
public static void main(String args[])
{
      CRectangle rect=new CRectangle(5,10);
      rect.show();

      CCircle cir=new CCircle(2.0);
      cir.show();
}
```

除了透過實作介面的類別建立物件的方式之外，我們也當然要談談 既然無法直接透過介面來建物件：

我們可以使用介面來宣告變數，甚至物件數量一多也可以宣告陣列：

```
public static void main(String args[])
{
      CShape shap01,shap02;    //透過介面宣告變數
      shap01=new CRectangle(5,10);
      shap01.show();

      shap02=new CCircle(2.0);
      shap02.show();
}
```

我們可以看到，透過介面型態的變數 shap01 shap02 來指向新建立的物件。\
不管main()中是使用何種方式，其執行結果皆為：\
area=50\
area=12.56

### 完整範例程式碼

```
interface CShape               // 定義介面 CShape
{
   final double PI=3.14;        //final 可省略
   public abstract void show(); //public abstract 可省略
}

class CCircle implementation CShape
{
   double radius;
   public CCircle(double r)   //CCircle建構元
   {
     radius=r;
   }
   public void show()         //改寫介面中抽象函數
   {
     System.out.println("面積＝"+PI*radius*radius);
   }
}
class CRectangle implements CShape
{
   int width,height;
   public CRectangle(int w,int h)  //CRectangle建構元
   {
      width=w;
      height=h;
   }
   public void show()
   {
      System.out.println("area="+width*height);
   }
}
public class interface01
{
   public static void main(String args[])
   {
      CShape shap01,shap02;    //透過介面宣告變數
      shap01=new CRectangle(5,10);
      shap01.show();

      shap02=new CCircle(2.0);
      shap02.show();
   }
}
```


---

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