欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > C#接口代码记录

C#接口代码记录

2025/8/12 19:50:44 来源:https://blog.csdn.net/m0_51648467/article/details/148591712  浏览:    关键词:C#接口代码记录
using System;namespace InterfacesExample
{// 定义接口public interface INBAPlayable{void KouLan();}public interface ISupermanable{void Fly();}// 基类public class Person{public void CHLSS(){Console.WriteLine("人类吃喝拉撒睡");}}// Student 类实现多个接口public class Student : Person, INBAPlayable, ISupermanable{public void KouLan(){Console.WriteLine("学生可以扣篮");}public void Fly(){Console.WriteLine("学生会飞");}public void Study(){// 示例方法}}// teacher 类实现多个接口public class Teacher : INBAPlayable, ISupermanable{public void Fly(){Console.WriteLine("教师会飞");}public void KouLan(){Console.WriteLine("教师会扣篮");}}class Program{static void Main(string[] args){INBAPlayable nBA = new Student();nBA.KouLan();INBAPlayable nBA1 = new Teacher();nBA1.KouLan();Person p = new Student();p.CHLSS();Console.ReadKey();}}
}

代码分析

  1. 接口定义
    • iNBAPlayable 接口定义了一个方法 KouLan()
    • iSupermanable 接口定义了一个方法 Fly()
  2. 类实现
    • Student 类继承自 Person 类,并实现了 iNBAPlayable 和 iSupermanable 接口。
    • teacher 类实现了 iNBAPlayable 和 iSupermanable 接口。
  3. 多态性
    • 在 Main 方法中,iNBAPlayable 接口类型的变量 nBA 和 nBA1 分别被赋值为 Student 和 teacher 类的实例。通过接口调用 KouLan() 方法,展示了多态性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词