c#中的接口查询(QueryInterface)

接口查询(QueryInterface)

       一个类可以有多个接口,声明了接口变量并且指向一个对象的时候,这个变量只能使用该接口内的方法和属性,而不能访问其他接口中的方法和属性。但接口查询很方便的让我们在一个类中的不同接口间进行切换。

using System;
namespace ConsoleApp1
{
        interface IEat
        {
            void Eat();
        }
        interface IRun
        {
            void Run();
        }
        class Person : IEat, IRun
        {
            public void Eat()
            {
                Console.WriteLine("我喜欢吃东西");
            }
            public void Run()
            {
                Console.WriteLine("我喜欢跑步");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                IEat eat = new Person();
                eat.Eat();
                IRun run = eat as IRun;
                run.Run();

                Console.ReadKey();
            }
        }
}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值