接口(入门)与多接口实现
将接口定义为契约,在一个类中实现多个接口,使用基于接口的多态,并学习显式接口实现基础。
接口(入门)与多接口实现 是 CoddyKit 上的免费 C# Academy 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。
接口概览
目标:面向接口编程,构建灵活的代码。
- 接口声明做什么,而不是如何做
- 实现多个接口
- 将接口传递给函数
- 使用显式实现解决名称冲突
实现多个接口
接口定义契约。一个类可以实现多个接口,以表达不同的角色。
using System;
public interface IPlayable
{
void Play();
}
public interface IHasDuration
{
int DurationSeconds { get; }
}
public class Song : IPlayable, IHasDuration
{
private readonly string _title;
private readonly int _seconds;
public Song(string title, int seconds)
{
_title = title;
_seconds = seconds;
}
public int DurationSeconds { get { return _seconds; } }
public void Play()
{
Console.WriteLine("Playing " + _title + " (" + _seconds + "s)");
}
}
public class Program
{
public static void Main(string[] args)
{
Song s = new Song("Hello", 5);
s.Play();
Console.WriteLine("Duration = " + s.DurationSeconds);
}
}
接口多态
在方法中接受接口类型。调用方可以传入任何实现。
using System;
public interface IPrintable
{
void Print();
}
public class Report : IPrintable
{
private readonly string _text;
public Report(string text) { _text = text; }
public void Print() { Console.WriteLine("Report: " + _text); }
}
public class Label : IPrintable
{
private readonly string _text;
public Label(string text) { _text = text; }
public void Print() { Console.WriteLine("Label: " + _text); }
}
public class Program
{
// Works with any IPrintable (polymorphism)
static void PrintAll(IPrintable item)
{
item.Print();
}
public static void Main(string[] args)
{
PrintAll(new Report("Sales Q1"));
PrintAll(new Label("Fragile"));
}
}
显式实现
显式实现可以消除同名成员的歧义,并将它们从类的公开表面隐藏起来。
using System;
public interface ILogger { void Write(string msg); }
public interface IFileWriter { void Write(string msg); }
public class DualWriter : ILogger, IFileWriter
{
// Explicit implementations: only visible via the interface reference
void ILogger.Write(string msg) { Console.WriteLine("[LOG] " + msg); }
void IFileWriter.Write(string msg) { Console.WriteLine("[FILE] " + msg); }
public void Demo()
{
Console.WriteLine("DualWriter ready");
}
}
public class Program
{
public static void Main(string[] args)
{
DualWriter dw = new DualWriter();
dw.Demo();
ILogger log = dw;
IFileWriter file = dw;
log.Write("started");
file.Write("saved");
}
}
接口集合
将不同的实现存储在接口类型的列表中,并以统一方式遍历。
using System;
using System.Collections.Generic;
public interface ITask
{
void Run();
}
public class EmailTask : ITask
{
public void Run() { Console.WriteLine("Send email"); }
}
public class BackupTask : ITask
{
public void Run() { Console.WriteLine("Run backup"); }
}
public class Program
{
public static void Main(string[] args)
{
List<ITask> tasks = new List<ITask>();
tasks.Add(new EmailTask());
tasks.Add(new BackupTask());
foreach (ITask t in tasks)
{
t.Run(); // calls the right implementation
}
}
}
接口提示
提示:
- 优先使用描述单一角色的小型接口。
- 使用接口进行测试(替换实现)。
- 在 C# 6 中,接口没有 fields 或方法体。
- 实现多个接口以组合行为。
接口的概念
回顾
回顾:接口是契约,并支持每个类实现多个接口。将接口传递给函数,保持接口小巧,并使用显式实现解决冲突。
常见问题解答
「接口(入门)与多接口实现」课时是免费的吗?
是的 — 「接口(入门)与多接口实现」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。
「接口(入门)与多接口实现」这节课中我会学到什么?
将接口定义为契约,在一个类中实现多个接口,使用基于接口的多态,并学习显式接口实现基础。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C# Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。
「接口(入门)与多接口实现」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C# Academy 课中编写并运行代码吗?
能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- virtual/override/sealed 与抽象类
- 接口(入门)与多接口实现
- 多态与动态分派