方法多載:同一類別中同名方法,參數型別或個數不同,編譯器依引數型別解析呼叫目標。
public Vehicle() {}
public Vehicle(int w, float g) { ... }
支援 int/float/string/object/char[]/格式化字串等,開發者無需記憶不同方法名。
public int MaxAge(int x, int y) { ... }
public float MaxWeight(float x, float y) { ... }
// 呼叫端需記住兩個名字
Max
public int Max(int x, int y) { ... }
public float Max(float x, float y) { ... }
// 呼叫端:s1.Max(s1.Age, s2.Age) / s1.Max(s1.Weight, s2.Weight)