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();
            }
        } 
    }

  • 相关阅读:
    Java IO 5 : 对象序列化
    Java IO 4 : RandomAccessFile
    3 Linux平台安装jenkins
    AWS-EC2配置swap
    2.8 环境准备-静态资源服务器搭建
    2.7 环境准备-MongoDB
    2.6 环境准备-redis
    2.5 环境准备-zookeeper
    2.4 环境准备-mysql8
    2.3 环境准备-nexus
  • 原文地址:https://www.cnblogs.com/zztong/p/6695226.html
Copyright © 2011-2022 走看看