c#之接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/boy_of_god/article/details/83280685

1、 接口的语法和特点

1.1、 接口的语法

  • 接口中的成员不允许添加修饰符,默认为public
  • 接口中不能写有方法体的方法//空实现也不行
  • 接口不能有字段和普通属性但是可以写自动属性
  • [public] interface I…able
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02接口的语法
{
   class Program
   {
       static void Main(string[] args)
       {
       }
   }
   public interface IFlayable
   {
       //public void Fly();//报错不能写修饰符默认为public
       void Flay();
       string Test();
       //void Methoed()//不能有方法体空实现也不行
       //{
       //}
       //string _name;
       string Name//可以有自动属性
       {
           get;
           set;
       }

   }

}

1.2接口的特点

  • 为了多态接口不能被实例化,也就是说接口不能被new创建对象抽象类也不能(创建对象没有意义,里面都是空的)
  • 接口与接口之间可以单继承继承,也可以多继承
  • 接口不能继承一个类,但是类可以继承一个接口,接口只能继承接口
  • 实现接口的类必须实现接口中的全部成员
  • 一个类可以继承一个类并同时实现多个接口,如果一个类同时继承了父类A,并实现接口AI,那么语法上A必须卸载AI的前面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04接口特点
{
   class Program
   {
       static void Main(string[] args)
       {
           //IFlyable fly = new IFlyable();接口不能被实例化//抽象类也不能被实例化
           //多态
           IFlyable fly = new Person();
           fly.Fly();
           IFlyable fly1 = new Bird();
           fly1.Fly();
           Console.ReadKey();
       }
   }
   public interface IFlyable
   {
       //不允许有访问修饰符,默认public
       //方法、自动属性
       void Fly();

   }
   public class Bird : IFlyable
   {


       public void Fly()
       {

           Console.WriteLine("我是鸟类我能飞");
       }
   }
   public class Person : IFlyable
   {
       public void Fly()
       {
           Console.WriteLine("我是人类我能飞");
       }
   }
   //继承的类应该卸载前面
   //public class IFlyable : Person
   //{
   //    public void Fly()
   //    {
   //        Console.WriteLine("我是人类我能飞");
   //    }
   //}
   //接口的继承
   public interface M1 
   {
       void Test1();
   }
   public interface M2
   {
       void Test2();
   }
   public interface M3
   {
       void Test3();
   }
   public interface SupperInterface : M1, M2, M3//接口可以继承多个接口
   { 
   }
   public class Car : SupperInterface//实现接口中的所有方法
   {

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

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

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

1.3接口的显示调用

1.3.1继承接口的类中本来就有与接口中的方法相同的方法此时调用的是不是接口中的方法

鸟类本身有一个会飞的方法,继承的接口中也有一个会飞的方法,
此时调用的是鸟类中本来就有的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06显示实现接口
{
    class Program
    {
        static void Main(string[] args)
        {
            IFlyable fly = new Bird();
            fly.Fly();
            Bird fly1 = new Bird();
            fly1.Fly();
            Console.ReadKey();
        }
    }
    public class Bird:IFlyable
    {
        public void Fly()
        {
            Console.WriteLine("我是鸟类自带的会飞的放方法鸟会飞");
        }
       
    }
    public interface IFlyable
    {
        void Fly();
    }
}

运行结果:
在这里插入图片描述

1.3.2 显示调用接口中的方法

** 接口名.方法名()** 例如: void IFlyable.Fly() {Console.WriteLine(“我是接口中的会飞方法”);}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06显示实现接口
{
    class Program
    {
        static void Main(string[] args)
        {
            IFlyable fly = new Bird();
            fly.Fly();
            Bird fly1 = new Bird();
            fly1.Fly();
            Console.ReadKey();
        }
    }
    public class Bird:IFlyable
    {
        public void Fly()
        {
            Console.WriteLine("我是鸟类自带的会飞的放方法鸟会飞");
        }
        void IFlyable.Fly()
        {
            Console.WriteLine("我是接口中的会飞方法");
        }
    }
    public interface IFlyable
    {
        void Fly();
    }
}

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/boy_of_god/article/details/83280685