Administrator
发布于 2025-10-14 / 4 阅读
0
0

接口(Interface)

一、接口是什么

接口(Interface)就是一份行为规范,它定义“能做什么”,但不管“怎么做”。

"接口定义了一套规范或契约,规定类必须做什么,但不关心具体怎么做"


二、示例

假设我们要设计一些“能发出声音”的东西,比如狗、猫、汽车。

我们先定义一个接口

public interface ISound
{
    void MakeSound(); // 定义一个行为:发出声音
}

这里的接口只是说明:

“谁要实现我(ISound),就必须写出一个 MakeSound() 方法。”


不同类实现接口:

public class Dog : ISound
{
    public void MakeSound()
    {
        Console.WriteLine("汪汪!");
    }
}
​
public class Car : ISound
{
    public void MakeSound()
    {
        Console.WriteLine("嘀嘀!");
    }
}

使用接口:

ISound s1 = new Dog();
ISound s2 = new Car();
​
s1.MakeSound(); // 输出:汪汪!
s2.MakeSound(); // 输出:嘀嘀!

无论是狗还是车,只要实现了 ISound, 我们就能用 ISound 这个“统一的接口”去调用它们的 MakeSound() 方法。


声明接口的语法

//"接口名称应以字母 I 开头,以指示该类型是接口。"
[访问修饰符] interface I接口名
{
    // 接口不能包含字段
    // 不能有:int x;
    
     // 属性也可以定义在接口中
     string 名称 { get; set; }
    
    // 编译错误:接口不能包含构造函数
    // public IExample() { }
    
    // 只有方法声明,没有实现
    void 绘制();
    string 获取颜色();
    // 接口不能包含具体实现
    // 不能有:void 方法() { Console.WriteLine("实现"); }
    
    
    // 只能包含这些成员:
    void Method();          // 方法
    string Property { get; } // 属性
    event EventHandler Event; // 事件
}

重点理解:

概念

说明

接口是什么

一种“约定”或“规范”

接口里能写什么

方法、属性、事件等的“声明”(不写具体实现)

谁能用接口

类或结构体可以“实现接口”

有什么好处

统一标准、方便扩展、支持多接口实现、解耦代码


💡 生活化理解:

接口就像插头标准

  • 你只要做出符合“三孔插头标准”的电器,

  • 就能插到任何“三孔插座”上使用。

  • 插座不关心你是“电风扇”还是“电脑”,只要“符合标准”就行。


示例1. 电源插座比喻

// 插座接口定义了一套规范
public interface I电源插座
{
    void 供电();  // 所有电器都要实现这个功能
}
​
// 不同电器实现相同的插座接口
public class 电视机 : I电源插座
{
    public void 供电()
    {
        Console.WriteLine("电视机获得电力,显示画面");
    }
}
​
public class 电冰箱 : I电源插座
{
    public void 供电()
    {
        Console.WriteLine("电冰箱获得电力,开始制冷");
    }
}

示例2. USB接口比喻

// USB接口标准
public interface IUSB设备
{
    void 传输数据();  // 所有USB设备都要实现
    void 连接电脑();  // 统一的行为规范
}
​
public class U盘 : IUSB设备
{
    public void 传输数据()
    {
        Console.WriteLine("U盘读写文件数据");
    }
    
    public void 连接电脑()
    {
        Console.WriteLine("U盘通过USB接口连接");
    }
}
​
public class 鼠标 : IUSB设备
{
    public void 传输数据()
    {
        Console.WriteLine("鼠标传输移动和点击数据");
    }
    
    public void 连接电脑()
    {
        Console.WriteLine("鼠标通过USB接口连接");
    }
}

示例3:一个类只能继承一个父类

public abstract class 动物
{
    public abstract void 叫();
    public void 呼吸()  // 可以有具体实现
    {
        Console.WriteLine("呼吸");
    }
}
​
// 问题:C#不支持多继承,一个类只能继承一个父类

示例3:一个类可以实现多个接口!

// 接口只包含规范,不包含实现
public interface I会叫的动物
{
    void 叫();  // 只有声明,没有实现
}
​
public interface I会飞的动物
{
    void 飞();
}
​
public interface I会游泳的动物
{
    void 游泳();
}
​
// 一个类可以实现多个接口!
public class 鸭子 : I会叫的动物, I会飞的动物, I会游泳的动物
{
    public void 叫()
    {
        Console.WriteLine("嘎嘎!");
    }
    
    public void 飞()
    {
        Console.WriteLine("鸭子扑腾翅膀飞行");
    }
    
    public void 游泳()
    {
        Console.WriteLine("鸭子在水中游泳");
    }
}

接口与抽象类的区别

特性

接口

抽象类

实现

只有声明,没有实现

可以包含具体实现

继承

可实现多个接口

只能继承一个抽象类

字段

不能包含字段

可以包含字段

构造函数

没有构造函数

有构造函数

使用场景

定义行为契约

提供部分实现的基类



评论