zoukankan      html  css  js  c++  java
  • C# Explicit 和 Implicit

        Implicit 关键字告诉编译器只有为了生成代码来调用方法,不需要在源代码中显示调用类型转换方法;

        Explicit 只有在显示转换时,才会调用方法;

    1. public sealed class Rational  
    2. {  
    3.     public Rational(Int32 num)  
    4.     {  
    5.     
    6.     }  
    7.     //Rational转成Int32  
    8.     public Int32 ToInt32()  
    9.     {  
    10.         return   
    11.     }  
    12.     //隐式调用  
    13.     public static implicit operator Rational(Int32 num)  
    14.     {  
    15.         return new Rational(num);  
    16.     }  
    17.     //显示调用  
    18.     public static explicit operator Int32(Rational r)  
    19.     {  
    20.         return r.ToInt32();  
    21.     }  
    22. }  

    调用例子:

    1. public sealed class Program  
    2. {  
    3.     public static void Main()  
    4.     {  
    5.         Rational r = 5;  
    6.         Int32 x = (Int32)r; //显示调用  
    7.     }  
    8. }  
  • 相关阅读:
    织梦DEDEcms首页调用文档整篇内容
    dedecms专题列表页不显示标题的解决办法
    怎么让织梦文章按照权重排序
    Codeforces274B
    HDU5693
    HDU2476
    POJ3613
    「LibreOJ NOIP Round #1」旅游路线
    Educational Codeforces Round 48
    组合博弈学习笔记
  • 原文地址:https://www.cnblogs.com/ILoveMyJob/p/10816436.html
Copyright © 2011-2022 走看看