zoukankan      html  css  js  c++  java
  • 运算符与表达式

    运算符和表达式跟 C++ 中完全一致。

    然而同时也添加了一些新的有用的运算符。有些在这里进行了讨论。

    is 运算符 

            is 运算符是用于检查操作数类型是否相等或可以转换。

    is 运算符特别适合用于多态的情形。

    is 运算符使用两个操作数,其结果是布尔值。

    参考例子: 

    void function(object param) 

    {  
        if(param is ClassA)        

             //做要做的事

     
        else if(param is MyStruct) 
            //做要做的事            

     } 

    }

     as 运算符 
            as 运算符检查操作数的类型是否可转换或是相等(as 是由 is 运算符完成的),如果是,则处理结果是已转换或已装箱的对象(如果操作数可以装箱为目标类型,参考 装箱/拆箱)。

    如果对象不是可转换的或可装箱的,返回值为 null。看看下面的例子以更好的理解这个概念。 

    Shape shp = new Shape();  
    Vehicle veh = shp as Vehicle; // 返回 null,类型不可转换 

    Circle cir = new Circle();  Shape shp = cir;  
    Circle cir2 = shp as Circle;  //将进行转换 

    object[] objects = new object[2]; objects[0] = "Aisha";

     object[1] = new Shape(); 
    string str; 
    for(int i="0"; i&< objects.Length; i++)

     { 
        str = objects as string;     if(str == null) 
            Console.WriteLine("can not be converted");    

     else 
            Console.WriteLine("{0}",str); 

    }  
    Output: Aisha 
    can not be converted   

  • 相关阅读:
    关于java中面向对象特征的总结
    Jemter TCP压测坑:Hex-encoded binary string contains an uneven no. of digits,Hex-encoded binary string contains an uneven no. of digits
    distinct&group by去重的区别
    Idea从gitee上clone项目时候相关问题
    Nginx正向代理
    docker安装MySQL5.7
    Ubuntu server18.04.5环境配置
    Ubuntu18.04.5 server wifi的连接
    git commit 提交规范
    关于js的学习的推介
  • 原文地址:https://www.cnblogs.com/neights/p/2949247.html
Copyright © 2011-2022 走看看