C#运算符重载和转换运算符实例(两个结构)
2020-07-18 04:55
816 查看
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); } }
转载于:https://www.cnblogs.com/zrj-xjyd/p/4915565.html
相关文章推荐
- c# DataTable与不同结构实体类转换的方法实例
- 【第二篇日记】C# 的转义字符,结构定义、字符转换、类型检查、运算符以及正则表达式
- C#编程基础(初级)(二)——转义字符,数学运算符,输入及其转换,goto语句,中断循环三种方法
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- C#中使用反射获取结构体实例及思路
- C++联合体转换成C#结构的实现方法
- c# 变量、运算符与类型转换
- C#实现Stream与byte[]之间的转换实例教程
- 第一章 ASP.net C#基本语法(B篇 程序结构、运算符、运算优先级)
- JAVA如何转换树结构数据代码实例
- C#类型转换总结(三)各类型转化实例
- C#中使用Lambda表达式自定义比较器实现两个列表合并实例
- 17.2015.08.04第十八节课 C#2 (数值类型及调用、引用类型及调用、装拆箱、常量、变量、数据类型转换、算术运算符、赋值运算符、关系运算符、逻辑运算符、字符串的常用方法)
- C# 两个独立exe程序直接通信 截取Windows消息来触发自定义事件 实例
- C#循环结构应用实例(二)
- C#中把任意类型的泛型集合转换成SQLXML数据格式的实例
- c# 对两个Datatable的结构相同进行合并,结构相同和不同
- XML 与 DataSet/DataTable 互相转换实例(C#)——转载
- C#编程实现对象与JSON串互相转换实例分析
- 黑马程序员—Java编程基础+常量与变量+类型的转换+进制的转换+运算符+scanner的使用+程序流程结构控制+循环结构