欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单的操作符重载,笔记

程序员文章站 2022-06-23 14:22:13
1 namespace 操作符重载 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Person person1 = new Person() { Name = "LiLei", Age = 12 }; 8 ... ......
 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}\nage'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 }