• .net -笔记 简单的操作符重载


    这个操作符重载很有意思

     1 class Program
     2 {
     3 static void Main(string[] args)
     4 {
     5 Person person1 = new Person() { Name = "LiLei", Age = 12 };
     6 Person person2 = new Person("HanMeimei", 11);
     7 Person person3 = person1 + person2;
     8 Person person4 = person1 == person2;
     9 Person person5 = 10;
    10 Console.WriteLine($"Name's merge is {person3.Name}
    Age's sum is {person3.Age}");
    11 Console.ReadKey();
    12 }
    13 }
    14 
    15 public class Person
    16 {
    17 public Person() { }
    18 public Person(string name, int age)
    19 {
    20 this.Name = name; this.Age = age;
    21 }
    22 public string Name { get; set; }
    23 public int Age { get; set; }
    24 public static Person operator +(Person p1, Person p2)
    25 {
    26 Person p3 = new Person(p1.Name + p2.Name, p1.Age + p2.Age);
    27 return p3;
    28 }
    29 
    30 public static Person operator ==(Person p1, Person p2)
    31 {
    32 Person p3 = new Person(p1.Name + p2.Name, p1.Age + p2.Age);
    33 return p3;
    34 }
    35 public static Person operator !=(Person p1, Person p2)
    36 {
    37 Person p3 = new Person(p1.Name + p2.Name, p1.Age + p2.Age);
    38 return p3;
    39 }
    40 
    41 public static implicit operator Person(int age)
    42 {
    43 return new Person();
    44 }
    45 }
    View Code
  • 相关阅读:
    JAVA 异常
    JAVA 接口的基本语法
    JAVA 访问权限
    Linux shell 函数应用示例02
    Linux shell 函数应用示例01
    Linux shell while循环语句
    Linux shell 中断循环语句
    Linux shell for循环结构
    测试用例基本概念
    软件测试原则
  • 原文地址:https://www.cnblogs.com/Darren-xia/p/9524424.html
Copyright © 2020-2023  润新知