C#運(yùn)算符重載實(shí)例解析
作者:xx22nn
C#運(yùn)算符重載實(shí)例向你講解了C#運(yùn)算符重載的應(yīng)用,具體的操作的內(nèi)容,希望對(duì)你有所幫助。
C#運(yùn)算符重載實(shí)例是學(xué)習(xí)C#運(yùn)算符重載的重要途徑。讓我們來(lái)看看下面的C#運(yùn)算符重載實(shí)例吧:
- using System;
- class hello
- ...{//邏輯運(yùn)算符 & | ! 按位運(yùn)算
- static void Main()
- ...{//rectangle長(zhǎng)方形[rek tan gl]
- string sName;int sInt;
- Console.Write("Input Name:"); sName=Console.ReadLine();
- Console.Write("Input a Number:"); sInt = int.Parse(Console.ReadLine());
- clsString myStr=new clsString();
- myStr.sL = sName; myStr++;//利用C#運(yùn)算符重載之重載的++運(yùn)算符使其轉(zhuǎn)變?yōu)榇髮?xiě)
- clsNot myNotStr = new clsNot();
- myNotStr.sL = sName; myNotStr=!myNotStr;//利用C#運(yùn)算符重載之重載的++運(yùn)算符使其轉(zhuǎn)變?yōu)榇髮?xiě)
- Console.WriteLine("The Enter Number is: {0}", sInt);
- clsInt myInt = new clsInt();
- myInt.sL = sInt; myInt++;
- object ob = myInt.sL;//裝箱
- AdverseCase mStr = new AdverseCase(); mStr.sL = sName;//利用類(lèi)的全局變量轉(zhuǎn)換
- string c = myStr;
- Console.WriteLine("Upper Name is: {0}", c);//隱含轉(zhuǎn)換成字符串
- Console.WriteLine("Your Name is: {0}", sName);
- Console.WriteLine("Not Name is: {0}", myNotStr.sL);
- Console.WriteLine("Cls Name is: {0}", mStr.ToAdverseCase());
- Console.WriteLine("The Number Increase is: {0}", ob);
- Console.ReadLine();
- }
- }
- class clsString//C#運(yùn)算符重載之重載++運(yùn)算符使字符串變大寫(xiě)
- //如果要重載++運(yùn)算符使其能直接用于string怎么處理
- ...{
- public string sL;
- public static clsString operator ++(clsString strIn)
- ...{
- clsString cs = new clsString();
- cs.sL = strIn.sL.ToUpper();
- return cs;
- }
- //隱含轉(zhuǎn)換public static implicit operator string(clsString c)
- //明確轉(zhuǎn)換public static explicit operator string(clsString c)
- //重載 類(lèi)型轉(zhuǎn)換 由clsString轉(zhuǎn)換為 string
- public static implicit operator string(clsString c)
- ...{
- string i; i = c.sL;
- return i;
- }
- }
- class clsInt//C#運(yùn)算符重載之重載++運(yùn)算符使整形數(shù)據(jù)每次增加10
- ...{
- public int sL;
- public static clsInt operator ++(clsInt strIn)
- ...{
- clsInt ci = new clsInt();
- ci.sL = strIn.sL+10;
- return ci;
- }
- }
- class clsNot//利用C#運(yùn)算符重載之運(yùn)算符 ! 重載轉(zhuǎn)換翻轉(zhuǎn)字符串大小寫(xiě)
- ...{
- public string sL;
- public static clsNot operator !(clsNot strIn)
- ...{
- clsNot ci = new clsNot();
- ci.sL = RevServer(strIn.sL);
- return ci;
- }
- protected static string RevServer(string s)
- ...{//大寫(xiě)變小寫(xiě),小寫(xiě)變大寫(xiě)
- char[] ct;
- if (s.Length == 1)
- ...{
- ct = s.ToCharArray();
- if (ct[0] >= 'a')s = s.ToUpper();else s = s.ToLower();
- return s;
- }
- string sTmp = s.Substring(0, 1);
- ct = sTmp.ToCharArray();
- if (ct[0] >= 'a')
- sTmp = sTmp.ToUpper();
- else
- sTmp = sTmp.ToLower();
- sTmp += RevServer(s.Substring(1));
- return sTmp;
- }
- }
- //利用類(lèi)成員轉(zhuǎn)換翻轉(zhuǎn)字符串大小寫(xiě)
- class AdverseCase
- ...{
- public string sL;
- public string ToAdverseCase()
- ...{
- return RevServer(this.sL);
- }
- protected static string RevServer(string s)
- ...{//大寫(xiě)變小寫(xiě),小寫(xiě)變大寫(xiě)
- char[] ct;
- if (s.Length == 1)
- ...{
- ct = s.ToCharArray();
- if (ct[0] >= 'a') s = s.ToUpper(); else s = s.ToLower();
- return s;
- }
- string sTmp = s.Substring(0, 1);
- ct = sTmp.ToCharArray();
- if (ct[0] >= 'a')
- sTmp = sTmp.ToUpper();
- else
- sTmp = sTmp.ToLower();
- sTmp += RevServer(s.Substring(1));
- return sTmp;
- }
- }
C#運(yùn)算符重載實(shí)例的內(nèi)容就向你介紹到這里,希望對(duì)你學(xué)習(xí)C#運(yùn)算符重載有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來(lái)源:
CSDN博客