C#(二十三)之C#中的is运算符

225 阅读5分钟

「这是我参与2022首次更文挑战的第22天,活动详情查看:2022首次更文挑战

1:is运算符:

is运算符用来判断对象是不是某种类型

        /* C#主要的运行函数,就是main函数 */
        static void Main(string[] args)
        {
            // is运算符
            double x = 100;
            Console.WriteLine(x is double);
            // 输出:True
        }

 

2:向下转换

基类转换为派生类:向下转换

派生类转换为基类:向上转换(这个在C#中没有必要)

/* C#主要的运行函数,就是main函数 */
        static void Main(string[] args)
        {
            // 向下类型装换
            B b = new B();
            A a = b;
        }
        class A {
            public void wordA()
            {
                Console.WriteLine("我是A");
            }
            public virtual void AA()
            {
                Console.WriteLine("我是AA");
            }
        }
        class B : A {
            public void wordB()
            {
                Console.WriteLine("我是B");
            }
            public override void AA()
            {
                Console.WriteLine("我是重写AA");
            }
        }

以上代码就实现了C#中的向下转换。

那么这是否就意味着,a类转化为b类的时候就可以直接使用b类中的方法呢?答案是不行。

普通方法不具有多态性,虚方法具有多态性。

那如果我们执行普通方法怎么办呢?两次握手强制转换

static void Main(string[] args)
        {
            // is运算符
            double x = 100;
            Console.WriteLine(x is double);
            // 输出:True
            // 向下类型装换
            B b = new B();
            A a = b;// 此时a并不是完全的B类对象
            // a.wordB();报错:普通方法不具有多态性
            a.AA();//可执行成功,虚方法具有多态性
            // 使用is运算符判断 a 的属性
            Console.WriteLine(a is B);
            if(a is B)
            {
                // 将具有不完全B属性的a对象强制转换为B对象
                B aa = (B)a;
                aa.wordB();  // 可运行成功
            }
           
        }

 

3:as运算符

上边的程序使用is运算符,这是没有问题的,但是麻烦了点,我们可以使用as运算符尝试下。

static void Main(string[] args)
        {
            // is运算符
            double x = 100;
            Console.WriteLine(x is double);
            // 输出:True
            // 向下类型装换
            B b = new B();
            A a = b;// 此时a并不是完全的B类对象
            // a.wordB();报错:普通方法不具有多态性
            a.AA();//可执行成功,虚方法具有多态性
            B aab = a as B; // 将a转换为B类的对象
            // 转换成功就是 new B  转换不成功就是null
            if(aab != null){
                aab.wordB();
            }
        }

 

4:接口interface

定义接口命名基本上以大写的 ‘I’ 开头,当然这个不是一定的。只是一种个人习惯。

创建接口流程

(1):单机项目菜单,选择添加新项。

(2):选择接口,输入文件名,点击创建。

 

C#中的接口定义不需要使用修饰词(public  private  protected);

但是其默认是public,不用写。

 

接口的对象可以调用类中的方法而无需强制转换。这是重点

接口类:InterfaceGc.cs

interface InterfaceGc
    {
        // 存款
        void PayIn(decimal amount);
        // 取款
        bool QuKuan(decimal amount);
        // 余额(只读)
        decimal Sheng{
            get;
        }
    }

 

InterfaceNew.cs

// 定义一个转账接口
    interface InterfaceNew : InterfaceGc
    {
        // 定义一个接口方法
        bool TranceTo(InterfaceGc destination,decimal amount);
    }

 

继承接口类:

// 建立一个银行账户类继承接口类
        class Bank : InterfaceGc {
            // 定义一个余额
            private decimal blanace;
            // 重写接口中定义好的存钱类
            public void PayIn(decimal amount)
            {
                blanace += amount;
            }
 
            // 重写接口中定义好的取钱类
            public bool QuKuan(decimal amount)
            {
                // 判断余额是否大于要取得钱数
                if (blanace > amount)
                {
                    blanace -= amount;
                    return true;
                }
                else
                {
                    Console.WriteLine("余额不足,取款失败");
                    return false;
                }
            }
            // 重写余额接口
            public decimal Sheng
            {
                get{
                    return blanace;
                }
            }
        }
 
// 定义一个转账的接口
        class Inter : InterfaceNew
        {
            // 定义一个余额
            private decimal blanace;
            // 重写接口中定义好的存钱类
            public void PayIn(decimal amount)
            {
                blanace += amount;
            }
 
            // 重写接口中定义好的取钱类
            public bool QuKuan(decimal amount)
            {
                // 判断余额是否大于要取得钱数
                if (blanace > amount)
                {
                    blanace -= amount;
                    return true;
                }
                else
                {
                    Console.WriteLine("余额不足,取款失败");
                    return false;
                }
            }
 
            // 重写余额接口
            public decimal Sheng
            {
                get
                {
                    return blanace;
                }
            }
 
            // 转账接口
            public bool TranceTo(InterfaceGc obj, decimal amount)
            {
                bool result = QuKuan(amount);
                if (result == true)
                {
                    obj.PayIn(amount);
                }
                return result;
            }
        }
    }

 

调用:接口

接口的对象可以调用类中的方法而无需强制转换。这是重点

        static void Main(string[] args)
        {
            // 调用继承接口的类(注意这个写法---多态)
            // 接口的对象可以调用类中的普通方法
            InterfaceGc myAccount = new Bank();
            InterfaceNew youAccount = new Inter();
            //我先存钱
            myAccount.PayIn(1000);
            //我再取钱
            myAccount.QuKuan(1200);
            // 你先存钱
            youAccount.PayIn(5000);
            // 你给我转账
            youAccount.TranceTo(myAccount, 500);
            // 查看我的余额
            decimal myYue;
            myYue = myAccount.Sheng;
            Console.WriteLine("我的余额:"+myYue);
            //查看你的余额
            decimal youYue;
            youYue = youAccount.Sheng;
            Console.WriteLine("你的余额:"+youYue);
        }

 

测试使用全部代码:

接口文件:InterfaceGc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gc
{
    interface InterfaceGc
    {
        // 存款
        void PayIn(decimal amount);
        // 取款
        bool QuKuan(decimal amount);
        // 余额(只读)
        decimal Sheng{
            get;
        }
    }
}

 

InterfaceNew.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gc
{
    // 定义一个转账接口
    interface InterfaceNew : InterfaceGc
    {
        // 定义一个接口方法
        bool TranceTo(InterfaceGc destination,decimal amount);
    }
}

 

主文件:program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gc
{
    class Program
    {
        static void Main(string[] args)
        {
            // 调用继承接口的类(注意这个写法---多态)
            // 接口的对象可以调用类中的普通方法
            InterfaceGc myAccount = new Bank();
            InterfaceNew youAccount = new Inter();
           //我先存钱
            myAccount.PayIn(1000);
            //我再取钱
            myAccount.QuKuan(1200);
            // 你先存钱
            youAccount.PayIn(5000);
            // 你给我转账
            youAccount.TranceTo(myAccount, 500);
            // 查看我的余额
            decimal myYue;
            myYue = myAccount.Sheng;
            Console.WriteLine("我的余额:"+myYue);
            //查看你的余额
            decimal youYue;
            youYue = youAccount.Sheng;
            Console.WriteLine("你的余额:"+youYue);
        }
        // 建立一个银行账户类继承接口类
        class Bank : InterfaceGc {
            // 定义一个余额
            private decimal blanace;
            // 重写接口中定义好的存钱类
            public void PayIn(decimal amount)
            {
                blanace += amount;
            }
            // 重写接口中定义好的取钱类
            public bool QuKuan(decimal amount)
            {
                // 判断余额是否大于要取得钱数
                if (blanace > amount)
                {
                    blanace -= amount;
                    return true;
                }
                else
                {
                    Console.WriteLine("余额不足,取款失败");
                    return false;
                }
            }
            // 重写余额接口
            public decimal Sheng
            {
                get{
                    return blanace;
                }
            }
        }
        // 定义一个转账的接口
        class Inter : InterfaceNew
        {
            // 定义一个余额
            private decimal blanace;
            // 重写接口中定义好的存钱类
            public void PayIn(decimal amount)
            {
                blanace += amount;
            }
            // 重写接口中定义好的取钱类
            public bool QuKuan(decimal amount)
            {
                // 判断余额是否大于要取得钱数
                if (blanace > amount)
                {
                    blanace -= amount;
                    return true;
                }
                else
                {
                    Console.WriteLine("余额不足,取款失败");
                    return false;
                }
            }
            // 重写余额接口
            public decimal Sheng
            {
                get
                {
                    return blanace;
                }
            }
            // 转账接口
            public bool TranceTo(InterfaceGc obj, decimal amount)
            {
                bool result = QuKuan(amount);
                if (result == true)
                {
                    obj.PayIn(amount);
                }
                return result;
            }
        }
    }
}

有好的建议,请在下方输入你的评论。

欢迎访问个人博客 guanchao.site

欢迎访问小程序:

在这里插入图片描述