zoukankan      html  css  js  c++  java
  • implicit 与 explicit 的区别(笔记)

        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实现的是隐式类型转换,在外部需要进行强类型转换
    

  • 相关阅读:
    【YbtOJ#911】欧拉函数
    【CF590E】Birthday
    打印控件的区别
    RPA教程
    UiPath培训教程
    RPA视频教程
    搭建samba服务
    kvm虚拟机在线扩容
    zabbix监控交换机
    UiPath Level3讲解
  • 原文地址:https://www.cnblogs.com/magic_evan/p/2205486.html
Copyright © 2011-2022 走看看