轉換操作符 與 轉換符

來源:互聯網
上載者:User
/*===============================** TestOperator.cs* // 轉換操作符// 重載操作符===============================*/using System;public class Complex{private int x;public Complex(int x){this.x=x;}public int X{get{return x;}set{x=value;}}//+ 操作符的重載public static Complex operator+(Complex c1,Complex c2){return new Complex(c1.X+c2.X);}//public Complex Add(Complex c){return (this+c);}//轉換符//由一個int32 隱式構造一個Complex 對象並返回public static implicit operator Complex(Int32 numertor){return new Complex(numertor);}//有一個Complex 顯示返回一個int32public static explicit operator Int32(Complex c){return c.X;}}public class App{public static void Main(string[] args){Complex c1=new Complex(90);Complex c2=new Complex(100);Complex c3=c1+c2;Complex c4=2000;Console.WriteLine((c1+c2).X.ToString());Console.WriteLine(c4.X.ToString());Console.Read();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.