public class RMB { public int Yuan = 0; public RMB(int yuan) { Yuan = yuan; } public static RMB operator +(RMB rmb1, RMB rmb2) {//一式 return new RMB(rmb1.Yuan + rmb2.Yuan); } public static implicit operator int(RMB rmb) {//二式:implicit实现的是强类型转换 return rmb.Yuan; } public static implicit operator RMB(int yuan) {//三式:implicit实现的是强类型转换 return new RMB(yuan); } public static explicit operator RMB(float yuan) {//四式:explicit实现的是隐式类型转换,在外部需要进行强类型转换 return new RMB((int)yuan); } }
调用
RMB rmb1 = new RMB(1); RMB rmb2 = new RMB(2); RMB rmb3 = rmb1 + rmb2;//一式 int yuan = rmb3;//二式 RMB rmb4 = 5;//三式:implicit实现的是强类型转换 RMB rmb5 = (RMB)5f;//四式:explicit实现的是隐式类型转换,在外部需要进行强类型转换