C#运算符重载与运算符继承

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
}
}

以上,注意返回值即可。