📜  C#|使用接口的多重继承

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

在多重继承中,一个类可以具有多个父类,并且可以从其所有父类继承功能。如下图所示,类C继承了类A和B的功能。

但是C#不支持多类继承。为了克服这个问题,我们使用接口来实现多个类的继承。借助该界面,类C(如上图所示)可以获得类A和B的功能。

示例1:首先,我们尝试将Geeks1Geeks2类的功能继承到GeeksforGeeks类中,然后编译器将给出错误信息,因为C#直接不支持多类继承。

// C# program to illustrate
// multiple class inheritance
using System;
using System.Collections;
  
// Parent class 1
class Geeks1 {
  
    // Providing the implementation
    // of languages() method
    public void languages()
    {
  
        // Creating ArrayList
        ArrayList My_list = new ArrayList();
  
        // Adding elements in the
        // My_list ArrayList
        My_list.Add("C");
        My_list.Add("C++");
        My_list.Add("C#");
        My_list.Add("Java");
  
        Console.WriteLine("Languages provided by GeeksforGeeks:");
        foreach(var elements in My_list)
        {
            Console.WriteLine(elements);
        }
    }
}
  
// Parent class 2
class Geeks2 {
  
    // Providing the implementation
    // of courses() method
    public void courses()
    {
  
        // Creating ArrayList
        ArrayList My_list = new ArrayList();
  
        // Adding elements in the
        // My_list ArrayList
        My_list.Add("System Design");
        My_list.Add("Fork Python");
        My_list.Add("Geeks Classes DSA");
        My_list.Add("Fork Java");
  
        Console.WriteLine("\nCourses provided by GeeksforGeeks:");
        foreach(var elements in My_list)
        {
            Console.WriteLine(elements);
        }
    }
}
  
// Child class
class GeeksforGeeks : Geeks1, Geeks2 {
}
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating object of GeeksforGeeks class
        GeeksforGeeks obj = new GeeksforGeeks();
        obj.languages();
        obj.courses();
    }
}

运行时错误:

但是我们可以使用接口将Geeks1和Geek2类的功能间接继承到GeeksforGeeks类中。如下图所示。

示例2: GFG1GFG2接口均由Geeks1和Geeks2类实现。现在,Geeks1和Geeks2类定义了language()和courses()方法。当GeeksforGeeks类继承GFG1和GFG2接口时,您无需重新定义language()courses()方法,只需简单地创建Geeks1Geeks2类的对象,并使用GeeksforGeeks类中的这些对象访问language()courses()方法即可。

// C# program to illustrate how to
// implement multiple class inheritance
// using interfaces
using System;
using System.Collections;
  
// Interface 1
interface GFG1 {
    void languages();
}
  
// Parent class 1
class Geeks1 : GFG1 {
  
    // Providing the implementation
    // of languages() method
    public void languages()
    {
  
        // Creating ArrayList
        ArrayList My_list = new ArrayList();
  
        // Adding elements in the
        // My_list ArrayList
        My_list.Add("C");
        My_list.Add("C++");
        My_list.Add("C#");
        My_list.Add("Java");
  
        Console.WriteLine("Languages provided by GeeksforGeeks:");
        foreach(var elements in My_list)
        {
            Console.WriteLine(elements);
        }
    }
}
  
// Interface 2
interface GFG2 {
    void courses();
}
  
// Parent class 2
class Geeks2 : GFG2 {
  
    // Providing the implementation
    // of courses() method
    public void courses()
    {
  
        // Creating ArrayList
        ArrayList My_list = new ArrayList();
  
        // Adding elements in the
        // My_list ArrayList
        My_list.Add("System Design");
        My_list.Add("Fork Python");
        My_list.Add("Geeks Classes DSA");
        My_list.Add("Fork Java");
  
        Console.WriteLine("\nCourses provided by GeeksforGeeks:");
        foreach(var elements in My_list)
        {
            Console.WriteLine(elements);
        }
    }
}
  
// Child class
class GeeksforGeeks : GFG1, GFG2 {
  
    // Creating objects of Geeks1 and Geeks2 class
    Geeks1 obj1 = new Geeks1();
    Geeks2 obj2 = new Geeks2();
  
    public void languages()
    {
        obj1.languages();
    }
  
    public void courses()
    {
        obj2.courses();
    }
}
  
// Driver Class
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating object of GeeksforGeeks class
        GeeksforGeeks obj = new GeeksforGeeks();
        obj.languages();
        obj.courses();
    }
}

输出:

Languages provided by GeeksforGeeks:
C
C++
C#
Java

Courses provided by GeeksforGeeks:
System Design
Fork Python
Geeks Classes DSA
Fork Java