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 }
调用代码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(100, 200);
3 Complex c2 = new Complex(100, 300);
4 Complex result = c1 + c2;
5 MessageBox.Show(c1 + "\n" + c2 + "\n" + result);
6![](https://www.cnblogs.com/Images/dot.gif)
2 Complex c1 = new Complex(100, 200);
3 Complex c2 = new Complex(100, 300);
4 Complex result = c1 + c2;
5 MessageBox.Show(c1 + "\n" + c2 + "\n" + result);
6
![](https://www.cnblogs.com/Images/dot.gif)