C# 用冒號 : 同時表示繼承類別與實作介面 (相當於 Java extends/implements)。基類必須在最左邊。
public class ColorPoint : Point, IDrawable { ... }
同 Java,C# 類別不支援多重繼承,但可實作多個介面達成同樣目的。
// 基類:封裝 x, y 座標
public class Point
{
private int x, y;
public Point() { x = 0; y = 0; }
public int X { get => x; set => x = value; }
public int Y { get => y; set => y = value; }
}
// 衍生類:繼承 Point 並新增顏色
using System.Drawing;
public class ColorPoint : Point
{
private Color screenColor;
public ColorPoint() => screenColor = Color.Red;
public Color ScreenColor { get => screenColor; set => screenColor = value; }
}
Point 以 private 欄位 + public 屬性封裝座標。ColorPoint 自動繼承 X/Y 屬性,只需新增 ScreenColor。get/set 存取器對應 Java 的 getter/setter,語法更簡潔。