自定义隐式类型转换 implicit
来源:互联网 发布:python logging 编辑:程序博客网 时间:2024/05/16 19:20
将上一篇文章中的explicit关键字换为implicit即可。
using System;using System.Collections;using System.Linq;using System.Text;using System.Collections.Generic;using System.Runtime.Serialization;namespace TestCS{ public struct Rectangle { public int Width, Height; public Rectangle(int width, int height) { this.Width = width; this.Height = height; } public void Draw() { for (int i = 0; i < Width; i++) { for (int j = 0; j < Height; j++) { Console.Write("*"); } } Console.WriteLine(); } public override string ToString() { return string.Format("Rectangle, Width is {0} Height is {1}", Width, Height); } } public struct Square { public int Length; public Square(int length) { Length = length; } public void Draw() { for (int i = 0; i < Length; i++) { for (int j = 0; j < Length; j++) { Console.Write("*"); } } Console.WriteLine(); } public override string ToString() { return string.Format("Sqare :length is{0} ", Length); } public static implicit operator Square(Rectangle r) { Square s = new Square(r.Height); return s; } public static implicit operator Square(int r) { Square s = new Square(r); return s; } public static implicit operator int(Square s) { return s.Length; } } class Program { static void Main(string[] args) { Console.WriteLine(" Rectangle"); Rectangle r = new Rectangle(4, 3); Console.WriteLine(r.ToString()); r.Draw(); Console.WriteLine("From Rectangle"); Square s = r; Console.WriteLine(s.ToString()); s.Draw(); Console.WriteLine("From int"); Square s2 = 3; Console.WriteLine(s2.ToString()); s2.Draw(); Console.WriteLine("to int"); int i = s2; Console.WriteLine(i.ToString()); } } }
- 自定义隐式类型转换 implicit
- implicit隐式转换
- Operator运算符重载与Implicit隐式类型转换
- Operator运算符重载与Implicit隐式类型转换
- scala的implicit关键字----不仅仅是隐式类型转换
- scala implicit 隐式转换
- scala implicit隐式转换
- Scala implicit隐式转换
- scala implicit 隐式转换
- scala implicit 隐式转换
- scala implicit 隐式转换
- scala implicit 隐式转换
- scala隐式转换implicit
- 自定义隐式转换 public static implicit operator 和显式转换
- 效率: 条款21 利用重载技术避免隐式类型转换(implicit type conversions)
- C# 自定义 implicit和explicit转换
- explicit和implicit属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换。
- c#自定义类型的转换方式operator,以及implicit(隐式)和explicit (显示)声明的区别
- Using SQLAlchemy with Pylons
- C# 具有背景栅格的绘曲线类(一)
- GCC用法(转)
- ClearCase故障处理一例:解决eclipsed状态的私有文件不能被删除
- Firefox 3 单日下载量正式载入吉尼斯世界记录
- 自定义隐式类型转换 implicit
- 牵手,朴素却让人新动的动作
- 学习J2ME编程需要掌握的七种技术
- 200元钱,搞笑人生
- 看看,你都可以做到么
- 对方网络非正常断开检测方法
- [VC]Visual C++ 2008(或加上Feature Pack)发行后到未装vc9上运行提示"应用程序配置不正确" 的解决方案
- 记录KMP算法,记录其经典之处。。。
- delphi中String,PChar,PByte,Array of Char,Array of Byte 转换