重載運算符-創(chuàng)新互聯

========================================運算符重載

創(chuàng)新互聯建站主要從事成都網站設計、成都網站制作、網頁設計、企業(yè)做網站、公司建網站等業(yè)務。立足成都服務伊春,10余年網站建設經驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:13518219792

-------------------------------------------------------------------------Vector.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
    public struct Vector//結構和類是相似的
    {
        public int x, y, z;
        public Vector(int x, int y, int z)//構造函數
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public Vector(Vector v)//構造函數
        {
            this.x = v.x;
            this.y = v.y;
            this.z = v.z;
        }
        public override string ToString()//重載tostring方法
        {
            return string.Format("x:{0};y:{1};z:{2}", x, y, z);
        }
        //重載運算符
        //第一個參數是運算符左邊的,第二個參數是運算符右邊的
        //+ - * / 是一樣的
        public static Vector operator *(Vector lhs, Vector rhs)
        {
            Vector v = new Vector(lhs);
            v.x *= rhs.x;
            v.y *= rhs.y;
            v.z *= rhs.z;
            return v;
        }
    }
}

 -------------------------------------------------------------------------主程序

  Vector v1 = new Vector(1, 2, 3);
            Vector v2 = new Vector(3, 2, 1);
            Console.WriteLine((v1 * v2).ToString());//輸出x:3;y:4;z:3
            Console.ReadKey();

========================================比較運算符重載

-------------------------------------------------------------------------Vector.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
    public struct Vector//結構和類是相似的
    {
        public int x, y, z;
        public Vector(int x, int y, int z)//構造函數
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public Vector(Vector v)//構造函數
        {
            this.x = v.x;
            this.y = v.y;
            this.z = v.z;
        }
        public override string ToString()//重載tostring方法
        {
            return string.Format("x:{0};y:{1};z:{2}", x, y, z);
        }
 
        //重載比較運算符
        //== 與!=   需成對出現
        //> 與 <    需成對出現
        //>=  與 <=  需成對出現
        public static bool operator ==(Vector lhs, Vector rhs)
        {
            Vector v = new Vector(lhs);
            if (v.x == rhs.x && v.y == rhs.y && v.z == rhs.z)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //重載比較運算符
        public static bool operator !=(Vector lhs, Vector rhs)
        {
            return !(lhs == rhs);
        }
    }
}

 -------------------------------------------------------------------------主程序

Vector v1 = new Vector(1, 2, 3);
            Vector v2 = new Vector(1, 2, 3);
            Console.WriteLine(v1 == v2);//true
            Console.ReadKey();

 重載運算符

另外有需要云服務器可以了解下創(chuàng)新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享文章:重載運算符-創(chuàng)新互聯
地址分享:http://muchs.cn/article12/cocdgc.html

成都網站建設公司_創(chuàng)新互聯,為您提供軟件開發(fā)、微信小程序、網站改版、網站排名、微信公眾號、云服務器

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都app開發(fā)公司