📜  C#|界面

📅  最后修改于: 2021-05-29 15:50:47             🧑  作者: Mango

像类一样, Interface可以将方法,属性,事件和索引器作为其成员。但是接口将仅包含成员的声明。接口成员的实现将由隐式或显式实现接口的类给出。

  • 接口指定类必须执行的操作,而不指定方法。
  • 接口不能有私有成员。
  • 默认情况下,Interface的所有成员都是公共的和抽象的。
  • 该接口将始终在关键字“ interface ”的帮助下进行定义。
  • 接口不能包含字段,因为它们代表数据的特定实现。
  • 借助接口可以实现多重继承,而不能借助类。

接口声明的语法:

interface  
{
    // declare Events
    // declare indexers
    // declare methods 
    // declare properties
}

实现接口的语法:

class class_name : interface_name

要声明接口,请使用interface关键字。它用于提供总体抽象。这意味着接口中的所有成员都用空主体声明,并且默认情况下是公共的和抽象的。实现接口的类必须实现接口中声明的所有方法。

  • 范例1:
    // C# program to demonstrate working of 
    // interface
    using System;
      
    // A simple interface
    interface inter1
    {
        // method having only declaration 
        // not definition
        void display();
    }
      
    // A class that implements interface.
    class testClass : inter1
    {
          
        // providing the body part of function
        public void display()
        {
            Console.WriteLine("Sudo Placement GeeksforGeeks");
        }
      
        // Main Method
        public static void Main (String []args)
        {
              
            // Creating object
            testClass t = new testClass();
              
            // calling method
            t.display();
        }
    }
    
    输出:
    Sudo Placement GeeksforGeeks
    
  • 范例2:
    // C# program to illustrate the interface
    using System;
      
    // interface declaration
    interface Vehicle {
          
        // all are the abstract methods.
        void changeGear(int a);
        void speedUp(int a);
        void applyBrakes(int a);
    }
      
    // class implements interface
    class Bicycle : Vehicle{
          
        int speed;
        int gear;
          
        // to change gear
        public void changeGear(int newGear)
        {
              
            gear = newGear;
        }
          
        // to increase speed
        public void speedUp(int increment)
        {
              
            speed = speed + increment;
        }
          
        // to decrease speed
        public void applyBrakes(int decrement)
        {
              
            speed = speed - decrement;
        }
          
        public void printStates() 
        {
            Console.WriteLine("speed: " + speed + 
                              " gear: " + gear);
        }
    }
      
    // class implements interface
    class Bike : Vehicle {
          
        int speed;
        int gear;
          
        // to change gear
        public void changeGear(int newGear)
        {
              
            gear = newGear;
        }
          
        // to increase speed
        public void speedUp(int increment)
        {
              
            speed = speed + increment;
        }
          
        // to decrease speed
        public void applyBrakes(int decrement){
              
            speed = speed - decrement;
        }
          
        public void printStates() 
        {
            Console.WriteLine("speed: " + speed + 
                              " gear: " + gear);
        }
          
    }
      
    class GFG {
          
        // Main Method
        public static void Main(String []args) 
        {
          
            // creating an instance of Bicycle 
            // doing some operations 
            Bicycle bicycle = new Bicycle();
            bicycle.changeGear(2);
            bicycle.speedUp(3);
            bicycle.applyBrakes(1);
              
            Console.WriteLine("Bicycle present state :");
            bicycle.printStates();
              
            // creating instance of bike.
            Bike bike = new Bike();
            bike.changeGear(1);
            bike.speedUp(4);
            bike.applyBrakes(3);
              
            Console.WriteLine("Bike present state :");
            bike.printStates();
        }
    }
    
    输出:
    Bicycle present state :
    speed: 2 gear: 2
    Bike present state :
    speed: 1 gear: 1
    

界面优势:

  • 它用于实现松散耦合。
  • 它用于实现整体抽象。
  • 实现基于组件的编程
  • 实现多重继承和抽象。
  • 接口为应用程序添加了即插即用的体系结构。