新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#操作符重載學(xué)習(xí)實(shí)踐操作
C#操作符重載學(xué)習(xí)實(shí)踐操作

成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、遂平網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為遂平等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class MyCls
- {
- public int X
- {
- get; //C#操作符重載
- set;
- }
- public int Y
- {
- get;
- set;
- }
- public override string ToString()
- {
- return string.Format("X={0},Y={1}", X, Y);
- }
- public override bool Equals(object obj)
- {
- MyCls a = obj as MyCls;
- return a.X == this.X && a.Y == this.Y;
- }
- public override int GetHashCode()
- { //C#操作符重載
- return X * Y;
- }
- public static MyCls operator +(MyCls a, MyCls b)
- {
- return new MyCls() { X = a.X + b.X, Y = a.Y + b.Y };
- }
- public static MyCls operator -(MyCls a, MyCls b)
- {
- return new MyCls { X = a.X - b.X, Y = a.Y - b.Y };
- }
- public static MyCls operator ++(MyCls a)
- {
- return new MyCls() { X = a.X++, Y = a.Y++ };
- }
- public static MyCls operator --(MyCls a)
- {
- return new MyCls() { X = a.X--, Y = a.Y-- };
- }
- public static bool operator ==(MyCls a, MyCls b)
- {
- return a.X == b.X && a.Y == b.Y;
- }
- public static bool operator !=(MyCls a, MyCls b)
- { //C#操作符重載
- return a.X != b.X && a.Y != b.Y;
- }
- public static void Main()
- {
- MyCls a = new MyCls { X = 1, Y = 1 };
- MyCls b = new MyCls { X = 2, Y = 2 };
- Console.WriteLine(a + b);
- Console.WriteLine(b - a);
- Console.WriteLine(b++);
- Console.WriteLine(a--);
- Console.WriteLine(a++ == b);
- Console.WriteLine(a!= b--);
- Console.ReadLine();
- } //C#操作符重載
- }
- }
C#操作符重載程序輸出結(jié)果:
- X=3,Y=3
- X=1,Y=1
- X=3,Y=3
- X=0,Y=0
- True
- False
C#操作符重載的學(xué)習(xí)應(yīng)用就向你介紹到這里,希望對你了解和學(xué)習(xí)C#操作符重載有所幫助。
本文標(biāo)題:C#操作符重載學(xué)習(xí)實(shí)踐操作
網(wǎng)頁路徑:http://www.dlmjj.cn/article/djdiidc.html


咨詢
建站咨詢
