1 namespace 操作符重载 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Person person1 = new Person() { Name = "LiLei", Age = 12 }; 8 Person person2 = new Person("HanMeimei", 11); 9 Person person3 = person1 + person2; 10 Console.WriteLine($"Name's merge is {person3.Name} Age's sum is {person3.Age}"); 11 Console.ReadKey(); 12 } 13 } 14 public class Person 15 { 16 public Person() { } 17 public Person(string name, int age) 18 { 19 this.Name = name; this.Age = age; 20 } 21 public string Name { get; set; } 22 public int Age { get; set; } 23 public static Person operator +(Person p1, Person p2) 24 { 25 Person p3 = new Person(p1.Name + p2.Name, p1.Age + p2.Age); 26 return p3; 27 } 28 } 29 30 }