zoukankan      html  css  js  c++  java
  • C#之运算符重载学习案例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace Override_test
    {
        public class ComplexClass
        {
            public class complex
            {
                private float real;
                private float img;
                public complex(float p, float q)     //构造函数
                {
                    real = p;
                    img = q;
                }
                public complex()     //构造函数
                {
                    real = 0;
                    img = 0;
                }
                public void Print()
                {
                    Console.WriteLine("{0}+{1}i", real, img);
                }
                public static complex operator +(complex Lhs, complex rhs)
                {
                    complex sum = new complex();
                    sum.real = Lhs.real + rhs.real;
                    sum.img = Lhs.img + rhs.img;
                    return sum;
                }
                public static complex operator -(complex Lhs, complex rhs)
                {
                    complex result = new complex();
                    result.real = Lhs.real - rhs.real;
                    result.img = Lhs.img - rhs.img;
                    return result;
                }
            }
            static void Main()
            {
                complex A = new complex(10.5f, 12.5f);
                complex B = new complex(8.0f, 4.5f);
                complex C;
                Console.Write("Complex Number A:");
                A.Print();
                Console.Write("Complex Number B:");
                B.Print();
                C = A + B;
                Console.Write("
    A+B=");
                C.Print();
                C = A - B;
                Console.Write("A-B=");
                C.Print();
            }
        } 
    }

  • 相关阅读:
    C++用于修饰的keyword
    UVa 884
    yii 使用 mongodb 小工具 YiiMongoDbSuite
    三种网络协议握手
    学习设计模式的前世今生
    B二分法
    链接链接新手变化需要注意哪些问题
    插值与拟合 课件链接
    UVa 740
    疯狂暑期学习计划~~~
  • 原文地址:https://www.cnblogs.com/zztong/p/6695226.html
Copyright © 2011-2022 走看看