【C#学习】接口 Interface

这篇博客详细介绍了C#中的接口Interface,包括接口与抽象类的区别,接口的定义和实现方式(隐式和显式),接口继承,以及接口在多态和类方法共存中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接口和抽象类

一、 抽象类

抽象类是特殊的类,只是不能被实例化;除此以外,具有类的其他特性;重要的是抽象类可以包括抽象方法,这是普通类所不能的。抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们。另外,抽象类可以派生自一个抽象类,可以覆盖基类的抽象方法也可以不覆盖,如果不覆盖,则其派生类必须覆盖它们。

二、 接口

接口是引用类型的,类似于类,和抽象类的相似之处有三点:
1、不能实例化;
2、包含未实现的方法声明;
3、派生类必须实现未实现的方法,抽象类是抽象方法,接口则是所有成员(不仅是方法包括其他成员);
4、 接口除了可以包含方法之外,还可以包含属性、索引器、事件,而且这些成员都被定义为公有的。除此之外,不能包含任何其他的成员,例如:常量、域、构造函数、析构函数、静态成员。
5、一个类可以直接继承多个接口,但只能直接继承一个类(包括抽象类)。
6、接口与接口之间可以继承,并且可以多继承 
7、接口并不能去继承一个类,而类可以继承接口 (接口只能继承于接口,而类既可以继承接口,也可以继承类) 
8、一个类可以同时继承一个类并实现多个接口,如果一个派生类同时继承了基类A,并实现了接口 IA ,那么语法上 A 必须写在 IA 的前面。
9、接口中的成员不能加“访问修饰符”,接口中的成员访问修饰符默认为public,且不能修改。实现的接口方法可以用 abstract 和 virtual修饰,表明抽象类和可重写性。
10、抽象类也可以实现接口,但接口成员必须映射到抽象类的抽象成员。抽象类的派生类如果是非抽象类,则必须通过方法重载来实现接口成员。

三、 抽象类和接口的区别

1.类是对对象的抽象,可以把抽象类理解为把类当作对象,抽象成的类叫做抽象类.而接口只是一个行为的规范或规定,微软的自定义接口总是后带able字段,证明其是表述一类类“我能做。。。”.抽象类更多的是定义在一系列紧密相关的类间,而接口大多数是关系疏松但都实现某一功能的类中.
2.接口基本上不具备继承的任何具体特点,它仅仅承诺了能够调用的方法;    
3.一个类一次可以实现若干个接口,但是只能扩展一个父类    
4.接口可以用于支持回调,而继承并不具备这个特点.    
5.抽象类不能被密封。  
6.抽象类实现的具体方法默认为虚的,但实现接口的类中的接口方法却默认为非虚的,当然您也可以声明为虚的.
7.(接口)与非抽象类类似,抽象类也必须为在该类的基类列表中列出的接口的所有成员提供它自己的实现。但是,允许抽象类将接口方法映射到抽象方法上。  
8.抽象类实现了oop中的一个原则,把可变的与不可变的分离。抽象类和接口就是定义为不可变的,而把可变的座位子类去实现。
9.好的接口定义应该是具有专一功能性的,而不是多功能的,否则造成接口污染。如果一个类只是实现了这个接口的中一个功能,而不得不去实现接口中的其他方法,就叫接口污染。  
10.尽量避免使用继承来实现组建功能,而是使用黑箱复用,即对象组合。因为继承的层次增多,造成最直接的后果就是当你调用这个类群中某一类,就必须把他们全部加载到栈中!后果可想而知.(结合堆栈原理理解)。同时,有心的朋友可以留意到微软在构建一个类时,很多时候用到了对象组合的方法。比如asp.net中,Page类,有Server Request等属性,但其实他们都是某个类的对象。使用Page类的这个对象来调用另外的类的方法和属性,这个是非常基本的一个设计原则。  
11.如果抽象类实现接口,则可以把接口中方法映射到抽象类中作为抽象方法而不必实现,而在抽象类的子类中实现接口中方法.

四、 抽象类和接口的使用

1. 如果预计要创建组件的多个版本,则创建抽象类。抽象类提供简单的方法来控制组件版本。
2.如果创建的功能将在大范围的全异对象间使用,则使用接口。如果要设计小而简练的功能块,则使用接口。
3.如果要设计大的功能单元,则使用抽象类.如果要在组件的所有实现间提供通用的已实现功能,则使用抽象类。  
4.抽象类主要用于关系密切的对象;而接口适合为不相关的类提供通用功能。

以下是我在网上看到的几个形象比喻,真的非常不错,呵呵:

1.飞机会飞,鸟会飞,他们都继承了同一个接口“飞”;但是F22属于飞机抽象类,鸽子属于鸟抽象类。
2. 就像铁门木门都是门(抽象类),你想要个门我给不了(不能实例化),但我可以给你个具体的铁门或木门(多态);而且只能是门,你不能说它是窗(单继承);一个门可以有锁(接口)也可以有门铃(多实现)。门(抽象类)定义了你是什么,接口(锁)规定了你能做什么(一个接口最好只能做一件事,你不能要求锁也能发出声音吧(接口污染)。

接口的使用

定义接口

接口使用 interface 关键字声明,它与类的声明类似。接口声明默认是 public 的。下面是一个接口声明的实例:

interface IMyInterface
{
    void MethodToImplement();
}

实现接口

接口的方法不能用访问限制修饰符, 在继承接口的类中,这些方法必须是public的。派生类实现接口分 隐式实现显式实现

1. 隐式实现

隐式实现是不带接口名的实现,直接在派生中定义 public 接口方法。在隐式实现中,接口方法可以用 abstract(表明是抽象类,由派生类提供实现),virtual修饰(表明可以为派生类重写),但不能用 sealed和 override修饰。隐式实现允许派生类对象直接使用接口方法,而显式实现只允许接口引用使用接口方法。
using System;

interface IMyInterface
{
    // 接口成员
    void MethodToImplement();
}

class InterfaceImplementer : IMyInterface // 实现接口
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer(); // 也可以用IMyInterface ilmp接口引用,注意和显式实现对比
        iImp.MethodToImplement();
    }

    public void MethodToImplement() // 必须是public
    {
        Console.WriteLine("MethodToImplement() called.");
    }
}

2. 显式实现

显式实现是在接口方法名前加上接口名,以指明实现的方法是哪个接口的方法。显示实现接口方法时,不能有任何修饰符,只能由接口引用类型来调用,显示实现明确指出了所实现的方法来自于哪一个接口,所以, 方法接口名必须是声明了这个方法的接口,而不能是其派生接口
using System;

interface IMyInterfaceBase
{
    void goo();
}

interface IMyInterface: IMyInterfaceBase
{
    void foo();
}


class InterfaceImplementer : IMyInterface
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.foo(); // InterfaceImplement.foo()
        IMyInterface im = iImp;
        im.foo(); // IMyInterface.foo()
    }
   
    //void IMyInterface.goo() // 错误
    void IMyInterfaceBase.goo() // 注意必须是声明接口的直接接口
    {
        Console.WriteLine("IMyInterfaceBase.foo()");
    }

    void IMyInterface.foo() // 必须是public
    {
        Console.WriteLine("IMyInterface.foo()"); 
    }

    void foo()
    {
        Console.WriteLine("InterfaceImplement.foo()");
    }

}
下面是一个派生实现两个接口的例子,用接口引用引用对象,调用的就是相对应的接口的接口方法。
using System;

interface IMyInterface_1
{
    void MethodToImplement();
}

interface IMyInterface_2
{
    void MethodToImplement();
}

class InterfaceImplementer : IMyInterface_1, IMyInterface_2
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();

        IMyInterface_1 i1 = iImp;
        i1.MethodToImplement();

        IMyInterface_2 i2 = iImp;
        i2.MethodToImplement();
    }

    void IMyInterface_1.MethodToImplement() //  不能有任何修饰符,等价于是 private 的
    {
        Console.WriteLine("IMyInterface_1.MethodToImplement() called.");
    }

    void IMyInterface_2.MethodToImplement() // 不能有任何修饰符,等价于是 private 的
    {
        Console.WriteLine("IMyInterface_2.MethodToImplement() called.");
    }
}
输出: 
IMyInterface_1.MethodToImplement() called.
IMyInterface_2.MethodToImplement() called.

接口继承

如果一个接口继承其他接口,那么实现类或结构就需要实现所有接口的成员。 接口和接口之间可以多继承。
using System;

interface IParentInterface
{
    void ParentInterfaceMethod();
}

interface IMyInterface : IParentInterface
{
    void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.MethodToImplement();
        iImp.ParentInterfaceMethod();
    }

    public void MethodToImplement()
    {
        Console.WriteLine("MethodToImplement() called.");
    }

    public void ParentInterfaceMethod()
    {
        Console.WriteLine("ParentInterfaceMethod() called.");
    }
}
输出:

MethodToImplement() called.
ParentInterfaceMethod() called.

接口与多态

用接口实现多态的例子:

1.  新建一个飞的接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
    //飞的接口
   public interface IFly
   {
       //接口中方法不用访问修饰符,因为CLR会自动添加,并且不能有方法体
       void Say();

   }
}
2.  创建一个吃的接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
    //吃的接口
   public interface IEat
   {
       //接口中方法不用访问修饰符,因为CLR会自动添加,并且不能有方法体
       void eat();
   }
}
3.  创建一个鸟类来实现飞的接口和吃的接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
    //鸟实现飞的接口和吃的接口 
    public class Grid:IFly,IEat   //注意  接口:接口叫继承  ,类:接口叫实现
    {

        //如果一个类实现了某个接口,就得实现该接口中所有的方法
        public void Say()
        {
            Console.WriteLine("鸟在飞");
        }

        public void eat()
        {
            Console.WriteLine("鸟在吃");
        }
    }
}
4. 创建一个飞机类实现飞的接口
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
    //飞机实现飞的接口
   public class Plan:IFly
    {

        public void Say()
        {
            Console.WriteLine("飞机在飞");
        }
    }
}
5. Main方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义飞接口数组实例化对象
            IFly[] iFlies =
            {
                new Grid(), 
                new Plan()
            };
            //循环数组调用方法实现多态
            foreach (IFly iFly in iFlies)
            {
                iFly.Say();
            }
            //鸟吃实例化对象
            IEat iEats = new Grid();
            //调用方法实现多态
            iEats.eat();
            
            Console.ReadLine();
        }
    }
}
结果: 

鸟在飞
飞机在飞
鸟在吃

接口方法和类方法共存

如果派生类定义了和接口方法一样的成员方法,则具体调用哪个方法,由引用类型决定。如果是接口类型引用,则调用接口方法;如果是类类型引用,则调用类型方法: 
using System;

interface IMyInterface
{
    void foo();
}

class InterfaceImplementer : IMyInterface
{
    static void Main()
    {
        InterfaceImplementer iImp = new InterfaceImplementer();
        iImp.foo(); // InterfaceImplement.foo()
        IMyInterface im = iImp;
        im.foo(); // IMyInterface.foo()
    }

    void IMyInterface.foo() // 必须是public
    {
        Console.WriteLine("IMyInterface.foo()"); 
    }

    void foo()
    {
        Console.WriteLine("InterfaceImplement.foo()");
    }

}
输出: 

InterfaceImplement.foo()
IMyInterface.foo()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值