C#运算符重载与运算符继承 2023-01-19 5 笔记 ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { MagicApprentice p1 = new(1), p2 = new(2); Apprentice p3 = new(); public void Start() { p3=p1 + p2; print(p3.age); } } class Apprentice { public int age; public string name; public Apprentice() { } public Apprentice(int age) { this.age = age; } public Apprentice(string name) { this.name = name; } public Apprentice(string name,int age) { this.name = name; this.age = age; } public void TellName() { Debug.Log("I'm " + name); } public static Apprentice operator +(Apprentice a1, Apprentice a2) { return new Apprentice(a1.age + a2.age); } } interface Magic { void Ino(); } class MagicApprentice : Apprentice,Magic { public void Ino() { Debug.Log("Master Spark!"); } public MagicApprentice() { } public MagicApprentice(int age) { this.age = age; } public MagicApprentice(string name) { this.name = name; } public MagicApprentice(string name, int age) { this.name = name; this.age = age; } } ``` 以上,注意返回值即可。 本文链接: https://shrinken.pw/crash-2023-01-19_56-fml.html