C#運算子多載和轉換運算子執行個體(兩個結構)

來源:互聯網
上載者:User

標籤:pos   轉換運算子   summary   ring   隱式   gpo   void   temp   explicit   

struct Celsius{  private float degrees;  public float Degrees  {    get { return this.degrees; }  }public Celsius(float temp){  this.degrees = temp;  Console.WriteLine("這是攝氏溫度的構造!");}public static Celsius operator +(Celsius x, Celsius y)//重載+運算子{  return new Celsius(x.degrees + y.degrees);}public static Celsius operator -(Celsius x, Celsius y)//重載-運算子{  return new Celsius(x.degrees - y.degrees);}/// <summary>/// 隱式將Celsius轉換成float/// </summary>/// <param name="c">Celsius型別參數</param>/// <returns>float類型傳回值</returns>public static implicit operator float(Celsius c){  return c.Degrees;}/// <summary>/// 隱式的將float轉換成Celsius/// </summary>/// <param name="f">float型別參數</param>/// <returns>Celsius類型傳回值</returns>public static implicit operator Celsius(float f){  Celsius c = new Celsius(f);  return c;}/// <summary>/// 轉換運算子,顯示的將Celsius類型轉換成Fahrenheit類型/// </summary>/// <param name="c">Celsius型別參數</param>/// <returns>Fahrenheit類型傳回值</returns>/// public static explicit operator Fahrenheit(Celsius c){  float fl=((9.0f / 5.0f) * c.Degrees + 32);//Celsius類型轉換成Fahrenheit的規則  Fahrenheit f = new Fahrenheit(fl);  return f;}public override string ToString()//重寫ToString{  return this.Degrees.ToString();}} struct Fahrenheit{  private float degrees;   public float Degrees  {    get { return this.degrees; }  }public Fahrenheit(float temp){  this.degrees = temp;  Console.WriteLine("這是華氏溫度的構造!");}public static Fahrenheit operator +(Fahrenheit x, Fahrenheit y){  return new Fahrenheit(x.degrees + y.degrees);}public static Fahrenheit operator -(Fahrenheit x, Fahrenheit y){  return new Fahrenheit(x.degrees - y.degrees);}public static implicit operator float(Fahrenheit c){  return c.Degrees;}public static implicit operator Fahrenheit(float f){  Fahrenheit fa = new Fahrenheit(f);  return fa;}public static explicit operator Celsius(Fahrenheit f){  float fl = (5.0f / 9.0f) * (f.Degrees - 32);  Celsius c = new Celsius(fl);  return c;}public override string ToString(){  return this.Degrees.ToString();}} class Program{static void Main(string[] args){  Fahrenheit f = new Fahrenheit(100f);  Console.WriteLine("{0} fahrenheit = {1} celsius", f.Degrees, (Celsius)f);  Celsius c = 32f;//隱式將float轉換成celsius  Console.WriteLine("{0} celsius = {1} fahrenheit",c.Degrees,(Fahrenheit)c);  Fahrenheit f2 = f + (Fahrenheit)c;//驗證Fahrenheit的+重載  Console.WriteLine("{0} + {1} = {2} fahernheit",f.Degrees,(Fahrenheit)c,f2.Degrees);  Fahrenheit f3 = 100f;//隱式將float轉換成Fahrenheit  Console.WriteLine("{0} fahrenheit",f3.Degrees);}}

 

C#運算子多載和轉換運算子執行個體(兩個結構)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.