🪵 進擊的巨人
尤米爾·弗里茲是最初的巨人之力擁有者。
在她死後,國王為了維持這股力量,命令她的三個女兒 ── 瑪莉亞、羅塞、希娜 ── 吃下分屍後的母體。於是,巨人之力被分散成 九大巨人:始祖、進擊、超大型、鎧甲、女巨人、獸、戰鎚、顎、車力。
為甚麼呢,因為諫山創實作了多型(Polymorphism):
- 同樣都是「巨人」,卻能展現出完全不同的外觀、能力與戰鬥風格。
- 基底類別
Titan
定義了巨人的共通特性,而每個具體巨人(ColossalTitan、ArmoredTitan…)則是子類別,實作或覆寫不同的行為
- 後期的 Eren 同時擁有「進擊的巨人」、「始祖巨人」、「戰鎚巨人」的能力,就像一個物件同時實作多個介面,或覆寫了不同的行為
- 共通限制:因為尤米爾只活了 13 年,她的繼承者也都受「13 年壽命」的約束,這就像基底類別定義的共同規則,所有子類別都必須遵守
🪵 Static (Compile-time) Polymorphism
不同角色都會「攻擊」,但攻擊方式可能帶有不同條件(武器、對象、力量)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class Warrior { public void Attack() { Console.WriteLine("普通士兵揮刀攻擊!"); }
public void Attack(string target) { Console.WriteLine($"士兵使用立體機動裝置攻擊 {target} 的後頸!"); }
public void Attack(int titanStrength) { Console.WriteLine($"巨人用力量 {titanStrength} 的拳頭揮擊!"); }
public void Attack(string weapon, int titanStrength) { Console.WriteLine($"戰鎚巨人用 {weapon} 發動力量 {titanStrength} 的攻擊!"); } }
|
同樣是 Attack(),但編譯器會根據呼叫時的參數,決定實際使用哪個版本。
- 普通士兵揮刀(Attack())。
- 士兵 + 立體機動裝置 → 特化對巨人的攻擊(Attack(string target))。
- 巨人肉搏(Attack(int titanStrength))。
- 戰鎚巨人召喚武器(Attack(string weapon, int titanStrength))。
👉 靜態多型的關鍵在於在程式「執行前」就能決定要呼叫哪個方法。
🪵 Dynamic (Runtime) Polymorphism
👉 Method Overriding = 不同巨人繼承相同基底,但行為不同
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public abstract class Titan { public virtual void Attack() { Console.WriteLine("巨人攻擊!"); } }
public class ColossalTitan : Titan { public override void Attack() { Console.WriteLine("超大型巨人:巨大爆炸!"); } }
public class ArmoredTitan : Titan { public override void Attack() { Console.WriteLine("鎧甲巨人:硬化衝撞!"); } }
Titan titan = new ColossalTitan(); titan.Attack();
|
動態多型(執行期多型) 則是透過 方法覆寫(Method Overriding) 來實現。父類別定義了一個方法,子類別可以「重新實作」它。呼叫時,程式會依照物件的實際型別來決定行為。
Titan 是 base class,呼叫 Attack() 時,真正跑的是物件實際型別的行為。這就像馬萊戰士們,雖然都叫「巨人」,但實際上是誰(鎧甲/超大型/獸),攻擊方式就完全不同。
🪵 Interface Polymorphism
每個巨人實作了 ITitanShifter,所以可以用同一種型別(ITitanShifter)去操作不同的巨人,保證他們一定具備「變身」與「繼承」的能力。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public interface ITitanShifter { void Transform(); void Inherit(); }
public class AttackTitan : ITitanShifter { public void Transform() => Console.WriteLine("進擊的巨人變身!"); public void Inherit() => Console.WriteLine("進擊的巨人之力被繼承。"); }
public class ColossalTitan : ITitanShifter { public void Transform() => Console.WriteLine("超大型巨人變身!"); public void Inherit() => Console.WriteLine("超大型巨人之力被繼承。"); }
|
另一個例子是,不管是 弗里茲王族 還是 雷伊斯王族,只要是王室血脈,就必須遵守「維護始祖之力的秘密」這個契約。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public interface IRoyalFamily { void ProtectFoundingTitan(); }
public class FritzRoyal : IRoyalFamily { public void ProtectFoundingTitan() => Console.WriteLine("弗里茲家族:守護始祖之力!"); }
public class ReissRoyal : IRoyalFamily { public void ProtectFoundingTitan() => Console.WriteLine("雷伊斯家族:守護始祖之力!"); }
|
🪵 Abstract Class Polymorphism
Abstract Class = 一部分是統一規則,一部分是各自實作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public abstract class TitanShifter { public abstract void SpecialMove();
public void CommonWeakness() { Console.WriteLine("弱點:後頸被切開會死亡!"); } }
public class FemaleTitan : TitanShifter { public override void SpecialMove() { Console.WriteLine("女巨人:吸引純潔巨人!"); } }
public class CartTitan : TitanShifter { public override void SpecialMove() { Console.WriteLine("車力巨人:長時間持續戰鬥!"); } }
|
抽象類別是「半成品模板」。
有些行為是統一的(所有巨人都有後頸弱點)。有些行為必須交由子類別去具體實作(各自的專屬技能)。這就像九大巨人「同源」於尤米爾,因此有共通的限制(壽命 13 年、後頸弱點),但各自的力量卻不同。
🪵 結語
巨人之力實現了各式各樣的多型
- Static Polymorphism:同名方法,不同參數 → 如攻擊方式依條件不同而改變。
- Dynamic Polymorphism:同樣呼叫 Attack(),不同巨人展現不同招式。
- Interface Polymorphism:不同類別遵守相同契約 → 巨人繼承者、王室血脈。
- Abstract Class Polymorphism:共通規則 + 專屬能力 → 九大巨人同源於尤米爾。
多型的核心精神,就是「在共通之中展現差異」。正如巨人世界中,不論如何分支,最後都能追溯到同一個源頭──尤米爾。
🪵 參考文章
Can someone explain to me all the different types of Polymorphism in C#?