📜  C#中抽象类和接口之间的区别

📅  最后修改于: 2021-05-29 22:59:12             🧑  作者: Mango

抽象类是在C#中实现抽象的一种方法。从不打算直接实例化Abstract类。此类必须包含至少一个抽象方法,该方法在类定义中由关键字或修饰符abstract标记。 Abstract类通常用于在类层次结构中定义基类。

例子:

// C# program to illustrate the
// concept of abstract class
using System;
  
// abstract class 'G'
public abstract class G {
  
    // abstract method 'gfg1()'
    public abstract void gfg1();
}
  
// class 'G' inherit
// in child class 'G1'
public class G1 : G {
  
    // abstract method 'gfg1()'
    // declare here with
    // 'override' keyword
    public override void gfg1()
    {
        Console.WriteLine("Class name is G1");
    }
}
  
// class 'G' inherit in
// another child class 'G2'
public class G2 : G {
  
    // same as the previous class
    public override void gfg1()
    {
        Console.WriteLine("Class name is  G2");
    }
}
  
// Driver Class
public class main_method {
  
    // Main Method
    public static void Main()
    {
  
        // 'obj' is object of class
        // 'G' class '
        // G' cannot
        // be instantiate
        G obj;
  
        // instantiate class 'G1'
        obj = new G1();
  
        // call 'gfg1()' of class 'G1'
        obj.gfg1();
  
        // instantiate class 'G2'
        obj = new G2();
  
        // call 'gfg1()' of class 'G2'
        obj.gfg1();
    }
}

输出 :

Class name is G1
Class name is  G2

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

例子:

// C# program to illustrate the
// concept of interface
using System;
  
// A simple interface
interface interface1 {
  
    // method having only declaration
    // not definition
    void show();
}
  
// A class that implements the interface.
class MyClass : interface1 {
  
    // providing the body part of function
    public void show()
    {
        Console.WriteLine("Welcome to GeeksforGeeks!!!");
    }
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating object
        MyClass obj1 = new MyClass();
  
        // calling method
        obj1.show();
    }
}

输出:

Welcome to GeeksforGeeks!!!

抽象类和接口之间的区别

Abstract Class Interface
It contains both declaration and definition part. It contains only a declaration part.
Multiple inheritance is not achieved by abstract class. Multiple inheritance is achieved by interface.
It contain constructor. It does not contain constructor.
It can contain static members. It does not contain static members.
It can contain different types of access modifiers like public, private, protected etc. It only contains public access modifier because everything in the interface is public.
The performance of an abstract class is fast. The performance of interface is slow because it requires time to search actual method in the corresponding class.
It is used to implement the core identity of class. It is used to implement peripheral abilities of class.
A class can only use one abstract class. A class can use multiple interface.
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class. If many implementations only share methods, then it is superior to use Interface.
Abstract class can contain methods, fields, constants, etc. Interface can only contain methods .
It can be fully, partially or not implemented. It should be fully implemented.