# 介面中的多重繼承

本篇的用意是，許多人會希望同時繼承多個父類別，如此一來便能使用到每個父類別的功能。\
但是 Java 卻又不允許這樣。

不過！透過 介面 就能達到多重繼承。

在先前的範例中，只有使用類別來實作一個介面，但其實類別可以實作兩個以上的介面。

做法很簡單：

```
class 類別名稱 implements 介面1名稱,介面2名稱,介面3名稱...
{
   ...
}
```

帶入實際的範例來更明白本篇的描述：取自上一篇［Java］介面 interface（基本範例解說）中範例\
CCircle 這個類別想要實作兩個介面，分別是CShape 與 CColor；一個是可用來計算面積、一個是設定顏色。

我們先定義這兩個介面（本範例僅定義兩個介面做示範）

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

```
interface CColor              // 定義介面 CColor
{
  abstract void setColor();
}
```

在上方我們定義了兩個介面，CShape 與 CColor；接下來要實作這兩個介面的寫法也很簡單：\
（以下撰寫程式的順序不為絕對方式，這是我自己的方式 僅供參考）

可先將大概的格式先撰寫出：

```
class CCircle implements CShape,CColor
{
   public void setColor()
   {
      ...
   }
   public void show()
   {
      ...
   }
}
```

因為分別繼承了來自兩個的抽象函數，因此我們也必須在子類別當中處理這兩樣函數的定義。

再來是為了設半徑 radius 給圓，撰寫一個建構元：

```
public CCircle(double r)
{
   radius=r;
}
```

為了讓在 main() 中可以正常呼叫 setColor 函數並設一個顏色（String），與呼叫show() 來顯示面積\
所以我們開始撰寫繼承來的兩個函數功能：

```
public void setColor(String str)
{
   color=str;
   System.out.println("color="+color);
}
public void show()
{
   System.out.println("area="+PI*radius*radius);
}
```

以下就能完成實作的程式碼了！

```
class CCircle implements CShape,CColor
{
   double radius;
   String color;
   public CCircle(double r)
   {
      radius=r;
   }
   public void setColor(String str)
   {
      color=str;
      System.out.println("color="+color);
   }
   public void show()
   {
      System.out.println("area="+PI*radius*radius);
   }
}
```

最後來到 main() 的部分：

```
public static void main(String args[])
{
    CCircle moon=new CCircle(5.0);
    moon.setColor("yellow");
    moon.show();
}
```

經過以上簡單的小程式演示了，使用類別來繼承多個介面 來達到多重繼承的目的。

最後附上完整程式碼：

```
interface CShape            // 定義介面 CShape
{
   final double PI=3.14;
   public abstract void show();
}
interface CColor              // 定義介面 CColor
{
  abstract void setColor();
}
class CCircle implements CShape,CColor
{
   double radius;
   String color;
   public CCircle(double r)
   {
      radius=r;
   }
   public void setColor(String str)
   {
      color=str;
      System.out.println("color="+color);
   }
   public void show()
   {
      System.out.println("area="+PI*radius*radius);
   }
}
public class interface02
{
    public static void main(String args[])
    {
      CCircle moon=new CCircle(5.0);
      moon.setColor("yellow");
      moon.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-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.
