1  public class Complex
 2   {
 3     int _real;
 4     int _imag;
 5     public Complex(int i1, int i2)
 6     {
 7       _real = i1;
 8       _imag = i2;
 9     }
10 
11     public int real
12     {
13       get { return _real; }
14       set { _real = value; }
15     }
16     public int imaginary
17     {
18       get { return _imag; }
19       set { _imag = value; }
20     }
21     public override string ToString()
22     {
23       return (string.Format("{0} + {1} i", real, imaginary));
24     }
25     //
26     public static Complex operator +(Complex c1, Complex c2)
27     {
28       return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
29     }
30   }
调用代码
1 
2 Complex c1 = new Complex(100200);
3 Complex c2 = new Complex(100300);
4 Complex result = c1 + c2;
5 MessageBox.Show(c1 + "\n" + c2 + "\n" + result);
6 运算符重载-示例程序

相关文章:

  • 2022-01-19
  • 2021-12-04
  • 2021-10-28
  • 2021-07-04
  • 2022-01-09
  • 2021-09-28
  • 2021-11-17
  • 2021-10-07
猜你喜欢
  • 2023-03-20
  • 2021-04-06
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
相关资源
相似解决方案