
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IDrivingLicenseB { void GetLicense(); } interface IDrivingLicenseA : IDrivingLicenseB { new void GetLicense(); } class Teacher : IDrivingLicenseA { public void GetLicense() { Console.WriteLine("Teacher got license A."); } } class Student : IDrivingLicenseB { public void GetLicense() { Console.WriteLine("Student got license B."); } } class Program { static void DriveCar(string name, IDrivingLicenseB o) { IDrivingLicenseB dl = o as IDrivingLicenseB; if (dl != null) { Console.WriteLine("drive car."); } else { Console.WriteLine("cannot drive car."); } } static void DriveBus(string name, IDrivingLicenseB o) { IDrivingLicenseA dl = o as IDrivingLicenseA; if (dl != null) { Console.WriteLine("drive bus."); } else { Console.WriteLine("cannot drive bus."); } } static void Main(string[] args) { Teacher t = new Teacher(); DriveCar("Teacher", t); DriveBus("Teacher", t); Student s = new Student(); DriveCar("Student", s); DriveBus("Student", s); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IDrivingLicenseB { void GetLicense(); } interface IDrivingLicenseA : IDrivingLicenseB { new void GetLicense(); } class Teacher : IDrivingLicenseA { void IDrivingLicenseA.GetLicense() { Console.WriteLine("Teacher got license A."); } void IDrivingLicenseB.GetLicense() { Console.WriteLine("Teacher got license B."); } public void GetLicense() { Console.WriteLine("VVVB."); } } class Student : IDrivingLicenseB { public void GetLicense() { Console.WriteLine("Student got license B."); } } class Program { static void DriveCar(string name, IDrivingLicenseB o) { IDrivingLicenseB dl = o as IDrivingLicenseB; if (dl != null) { Console.WriteLine("drive car."); } else { Console.WriteLine("cannot drive car."); } } static void DriveBus(string name, IDrivingLicenseB o) { IDrivingLicenseA dl = o as IDrivingLicenseA; if (dl != null) { Console.WriteLine("drive bus."); } else { Console.WriteLine("cannot drive bus."); } } static void Main(string[] args) { Teacher t = new Teacher(); ((IDrivingLicenseA)t).GetLicense(); ((IDrivingLicenseB)t).GetLicense(); t.GetLicense(); //DriveCar("Teacher", t); //DriveBus("Teacher", t); //Student s = new Student(); //DriveCar("Student", s); //DriveBus("Student", s); Console.ReadKey(); } } }
完全限定名不能用public修饰符修饰。去掉public,即为private。
接口的多继承:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IA { void F(); } interface IB : IA { new void F(); } interface IC : IA { void G(); } interface IBC : IB, IC { } class D: IBC { public void F() { Console.WriteLine("IB.F()"); } public void G() { Console.WriteLine("IC.G()"); } } class Program { static void Main(string[] args) { D d = new D(); ((IBC)d).F(); ((IB)d).F(); ((IC)d).F(); ((IA)d).F(); Console.ReadKey(); } } }
Note:
调用IC的F方法,IC的F方法继承自IA接口。接口多重继承原则:直观隐藏规则。如果成员在任何一个路径中被隐藏,则他在任何一个路径中也被隐藏掉。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IA { void F(); } class B: IA { void IA.F() { Console.WriteLine("B.F"); } } class C : B, IA { public void F() { Console.WriteLine("C.F"); } } class Program { static void Main(string[] args) { C c = new C(); ((IA)c).F(); //((B)c).F(); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IA { void F(); } class B: IA { void IA.F() { Console.WriteLine("B.F"); } } class C : B, IA { public void F() { Console.WriteLine("C.F"); } } class Program { static void Main(string[] args) { C c = new C(); ((IA)c).F(); B b = c; ((IA)b).F(); Console.ReadKey(); } } }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具