自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#運(yùn)算符重載實(shí)例解析

開(kāi)發(fā) 后端
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í)例吧:

  1. using System;  
  2. class hello  
  3. ...{//邏輯運(yùn)算符 & | ! 按位運(yùn)算  
  4.     static void Main()  
  5.     ...{//rectangle長(zhǎng)方形[rek tan gl]  
  6.         string sName;int sInt;  
  7.     Console.Write("Input Name:");       sName=Console.ReadLine();  
  8.     Console.Write("Input a Number:");   sInt = int.Parse(Console.ReadLine());  
  9.  
  10.       
  11.     clsString myStr=new clsString();      
  12.         myStr.sL = sName;   myStr++;//利用C#運(yùn)算符重載之重載的++運(yùn)算符使其轉(zhuǎn)變?yōu)榇髮?xiě)  
  13.     clsNot myNotStr = new clsNot();  
  14.     myNotStr.sL = sName; myNotStr=!myNotStr;//利用C#運(yùn)算符重載之重載的++運(yùn)算符使其轉(zhuǎn)變?yōu)榇髮?xiě)  
  15.       
  16.     Console.WriteLine("The Enter Number is: {0}", sInt);  
  17.     clsInt myInt = new clsInt();          
  18.         myInt.sL = sInt;    myInt++;  
  19.     object ob = myInt.sL;//裝箱  
  20.  
  21.     AdverseCase mStr = new AdverseCase(); mStr.sL = sName;//利用類(lèi)的全局變量轉(zhuǎn)換  
  22.     string c = myStr;  
  23.     Console.WriteLine("Upper Name is: {0}", c);//隱含轉(zhuǎn)換成字符串  
  24.     Console.WriteLine("Your  Name is: {0}", sName);  
  25.     Console.WriteLine("Not   Name is: {0}", myNotStr.sL);  
  26.     Console.WriteLine("Cls   Name is: {0}", mStr.ToAdverseCase());  
  27.     Console.WriteLine("The Number Increase is: {0}", ob);  
  28.     Console.ReadLine();  
  29.     }  
  30. }  
  31. class clsString//C#運(yùn)算符重載之重載++運(yùn)算符使字符串變大寫(xiě)
  32. //如果要重載++運(yùn)算符使其能直接用于string怎么處理  
  33. ...{  
  34.     public string sL;  
  35.     public static clsString operator ++(clsString strIn)  
  36.     ...{  
  37.         clsString cs = new clsString();  
  38.         cs.sL = strIn.sL.ToUpper();  
  39.         return cs;  
  40.     }  
  41.     //隱含轉(zhuǎn)換public static implicit operator string(clsString c)  
  42.     //明確轉(zhuǎn)換public static explicit operator string(clsString c)  
  43.     //重載 類(lèi)型轉(zhuǎn)換 由clsString轉(zhuǎn)換為 string                       
  44.     public static implicit operator string(clsString c)  
  45.     ...{  
  46.         string i; i = c.sL;  
  47.         return i;  
  48.     }  
  49. }  
  50. class clsInt//C#運(yùn)算符重載之重載++運(yùn)算符使整形數(shù)據(jù)每次增加10  
  51. ...{  
  52.     public int sL;  
  53.     public static clsInt operator ++(clsInt strIn)  
  54.     ...{  
  55.         clsInt ci = new clsInt();  
  56.         ci.sL = strIn.sL+10;  
  57.         return ci;  
  58.     }  
  59. }  
  60. class clsNot//利用C#運(yùn)算符重載之運(yùn)算符 ! 重載轉(zhuǎn)換翻轉(zhuǎn)字符串大小寫(xiě)  
  61. ...{  
  62.     public string sL;  
  63.     public static clsNot operator !(clsNot strIn)  
  64.     ...{  
  65.         clsNot ci = new clsNot();  
  66.         ci.sL = RevServer(strIn.sL);  
  67.         return ci;  
  68.     }  
  69.     protected static string RevServer(string s)  
  70.     ...{//大寫(xiě)變小寫(xiě),小寫(xiě)變大寫(xiě)  
  71.         char[] ct;  
  72.         if (s.Length == 1)  
  73.         ...{  
  74.             ct = s.ToCharArray();   
  75.             if (ct[0] >= 'a')s = s.ToUpper();else s = s.ToLower();  
  76.             return s;  
  77.         }  
  78.         string sTmp = s.Substring(0, 1);  
  79.         ct = sTmp.ToCharArray();   
  80.         if (ct[0] >= 'a')  
  81.             sTmp = sTmp.ToUpper();  
  82.         else 
  83.             sTmp = sTmp.ToLower();  
  84.         sTmp +=  RevServer(s.Substring(1));  
  85.         return sTmp;  
  86.     }  
  87. }  
  88. //利用類(lèi)成員轉(zhuǎn)換翻轉(zhuǎn)字符串大小寫(xiě)  
  89. class AdverseCase  
  90. ...{  
  91.     public string sL;  
  92.     public string ToAdverseCase()  
  93.     ...{  
  94.         return RevServer(this.sL);  
  95.     }  
  96.     protected static string RevServer(string s)  
  97.     ...{//大寫(xiě)變小寫(xiě),小寫(xiě)變大寫(xiě)  
  98.         char[] ct;  
  99.         if (s.Length == 1)  
  100.         ...{  
  101.             ct = s.ToCharArray();  
  102.             if (ct[0] >= 'a') s = s.ToUpper(); else s = s.ToLower();  
  103.             return s;  
  104.         }  
  105.         string sTmp = s.Substring(0, 1);  
  106.         ct = sTmp.ToCharArray();  
  107.         if (ct[0] >= 'a')  
  108.             sTmp = sTmp.ToUpper();  
  109.         else 
  110.             sTmp = sTmp.ToLower();  
  111.         sTmp += RevServer(s.Substring(1));  
  112.         return sTmp;  
  113.     }  
  114. }  

C#運(yùn)算符重載實(shí)例的內(nèi)容就向你介紹到這里,希望對(duì)你學(xué)習(xí)C#運(yùn)算符重載有所幫助。

【編輯推薦】

  1. C#運(yùn)算符種類(lèi)簡(jiǎn)析
  2. C#位運(yùn)算符種類(lèi)及使用淺析
  3. C#運(yùn)算符重載實(shí)例淺析
  4. C#運(yùn)算符重載概念及應(yīng)用詳解
  5. C#運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算
責(zé)任編輯:仲衡 來(lái)源: CSDN博客
相關(guān)推薦

2009-08-12 10:27:12

C#運(yùn)算符重載運(yùn)算符重載實(shí)例

2009-08-12 10:47:03

C#運(yùn)算符重載

2009-09-04 13:18:10

C#允許運(yùn)算符重載

2009-08-14 10:16:57

C#運(yùn)算符重載

2009-08-12 12:46:11

C#運(yùn)算符重載

2009-08-12 10:37:13

C#運(yùn)算符重載

2009-08-11 15:51:08

C#運(yùn)算符算術(shù)運(yùn)算符

2009-08-12 11:20:51

C#運(yùn)算符重載

2009-08-12 09:30:10

C#??運(yùn)算符

2009-08-12 15:02:49

C#賦值運(yùn)算符簡(jiǎn)單賦值運(yùn)算符

2009-08-12 15:20:18

C#賦值運(yùn)算符復(fù)合賦值運(yùn)算符

2009-09-01 10:08:57

C#運(yùn)算符

2009-08-12 13:35:22

C#關(guān)系運(yùn)算符

2009-08-12 14:29:32

C#條件運(yùn)算符

2009-08-11 14:16:38

C# New運(yùn)算符

2009-08-12 14:49:33

C#移位運(yùn)算符

2009-11-06 13:57:52

C#

2021-12-15 10:25:57

C++運(yùn)算符重載

2011-07-15 01:34:36

C++重載運(yùn)算符

2009-08-12 10:07:51

C#運(yùn)算符
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)