C# 接口(interface 定义接口的关键字)

发布于:2025-07-30 ⋅ 阅读:(11) ⋅ 点赞:(0)

目录

使用接口案例

接口继承

练习


定义一个接口,在语法中与定义一个抽象类是没有区别的,但是不允许提供接口中任意成员的实现方式,一般接口只会包含方法 、索引器和事件的声明, 不允许声明成员的修饰符, public都不行 。因为接口总是共有的 ,也不能声明虚拟和静态。 

接口不会有构造函数  也不能有数据字段

接口不能被实例化

使用接口案例

定义接口  IFiy

//interface 定义接口的关键字
internal interface IFiy
{
    //定义没有实现的方法
    //飞  可以有参数
    void Fly();
    //攻击
    void Attack();

}

定义一个Plane飞机类继承 IFiy接口

internal class Plane : IFiy
{
    public void Attack()
    {
        Console.WriteLine("飞机在空中战斗");
    }

    public void Fly()
    {
        Console.WriteLine("飞机在空中飞");
    }
}

定义一个 Brid鸟类继承 IFiy接口,接口可以被多个类继承

internal class Brid : IFiy
{
    public void Fly()
    {
        Console.WriteLine("小鸟在空中飞");
    }

    public void Attack()
    {
        Console.WriteLine("小鸟在空中捕猎");
    }
}

主函数

static void Main(string[] args)
{
    // 接口不会有构造函数  也不能有数据字段
    Plane plane = new Plane();
    plane.Fly();


    //接口不能被实例化;
    //使用接口的具体操作
    IFiy fiy;
    fiy = new Plane();
    fiy.Fly();// 飞机的飞


    fiy = new Brid();
    fiy.Fly();//小鸟的飞

    //使用接口创建一个变量  写了两个构造 这在编程中叫做多态  
//因为fiy发生了变化 之前是飞机  后面变成了小鸟  所以他的形态是可以变化的 这种语法在C#中叫做  多态
}

接口继承

接口与接口之间的继承

internal interface Interface1
{
    void YY();
}
internal interface Interface2
{
    void cy();
}
//接口与接口之间的继承
internal interface Interface3 : Interface1 , Interface2
{
    void Show();
}
 //接口Interface3中含有三种方法,一个自己的两个继承的
internal class Class1 : Interface3
 {
     public void cy()
     {
         throw new NotImplementedException();
     }

     public void Show()
     {
         throw new NotImplementedException();
     }

     public void YY()
     {
         throw new NotImplementedException();
     }
 }
//定义一个抽象类模板
internal abstract class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Addres {  get; set; }
    public string Phone {  get; set; }
    public int  Age { get; set; }  
}
 // ,Interface1 接口继承
 //在继承中 除了第一个要继承的类或则是接口使用:  后面再继承的接口要使用逗号隔开

 // 类是不允许多继承的  每个类只能有一个基类
 internal class Yse : Student, Interface1 , Interface2
 {
     public Yse(string name) 
     {
         Name = name;
     
     }
     public void YY()
     {
         Console.WriteLine($"{Name}是会游泳的学生");
     }

     public void cy()
     {
         Console.WriteLine($"{Name}会唱歌");
     }
 }
internal class No : Student , Interface2
{
    public void cy()
    {
        Console.WriteLine($"{Name}会跳舞");
    }
}
static void Main(string[] args)
{
    Yse y = new Yse("张三");
    y.Name = "张三";
    y.YY();
    y.cy();
                
    Interface1 i;
    i = new Yse("张三");
    i.YY();

    // 接口的多态
    Interface1 i1;
    i1 = new Yse("李四");
    i1.YY();

}

练习

定义一个接口用来做数学运算 既可以做加法也可以做减法  通过具体的数学操作类(加法类 减法类)去实现接口中的方法 完成操作  提示:运用接口的多态性

主函数

internal class Program
{
    static void Main(string[] args)
    {
        Math m;
        m = new Add();
        Console.WriteLine(m.Maths(10,20)); // 30  加法  

        m = new Sub();
        Console.WriteLine(m.Maths(10,20)); // -10  减法
    }
}

创建一个Math接口

internal interface Math
{
    double Maths(double a , double b);
    
}

加法类

internal class Add :Math
{
    public double Maths(double a,double b)
    {
        return a + b;
    }
}

减法类

internal class Sub : Math
{
    public double Maths(double a, double b)
    {
        return a - b;
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到