📜  C# 程序在多个类中实现相同的方法

📅  最后修改于: 2022-05-13 01:55:22.072000             🧑  作者: Mango

C# 程序在多个类中实现相同的方法

C# 是一种通用编程语言,用于创建移动应用程序、桌面应用程序、网站和游戏。在 C# 中,对象是现实世界的实体。或者换句话说,对象是在运行时创建的运行时实体。它是一个类的实例。在本文中,在多个类中实现相同的方法可以通过使用接口来实现在多个类中实现相同的方法。 C# 中的接口就像类一样,它也有方法、属性、索引器和事件。但它只包含成员的声明,其成员的实现将由隐式或显式实现接口的类给出。或者我们可以说接口内部声明的所有方法都是抽象方法。它用于实现类无法实现的多重继承。

方法:

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。

示例 1:

C#
// C# program to illustrate the above concept
using System;
  
interface Interface
{
      
    // Declaring Method
    void greet();
}
  
// Creating classes and define the method 
// greet() in all classes.
class Class1 : Interface
{
    public void greet()
    {
        Console.WriteLine("Welcome Geeks");
    }    
}
  
class Class2 : Interface
{
    public void greet()
    {
        Console.WriteLine("Hello Geeks");
    }    
}
  
class Class3 : Interface
{
    public void greet()
    {
        Console.WriteLine("Bella Ciao Geeks");
    }    
}
  
class GFG{
      
public static void Main(String[] args)
{
      
    // I is the reference variable of Interface
    Interface I;
      
    // Initializing the I with objects of all the
    // classes one by one and calling the method greet()
    I = new Class1();
    I.greet();
  
    I = new Class2();
    I.greet();
      
    I = new Class3();
    I.greet();
}
}


C#
// C# program to illustrate the above concept
using System;
  
// Interface
interface Interface
{
      
    // Declaring Method 
    void greet();
}
  
// Creating classes and define the method
// greet() in all classes.
class Class1 : Interface
{
    public void greet()
    {
        Console.WriteLine("Welcome Geeks");
    }    
}
  
class Class2 : Interface
{
    public void greet()
    {
        Console.WriteLine("Hello Geeks");
    }    
}
  
class Class3 : Interface
{
    public void greet()
    {
        Console.WriteLine("Bella Ciao Geeks");
    }    
}
  
class Class4 : Interface
{
    public void greet()
    {
        Console.WriteLine("hola geeks");
    }    
}
  
class Class5 : Interface
{
    public void greet()
    {
        Console.WriteLine("welcome to geeksforgeeks");
    }
}
  
class GFG{
      
public static void Main(String[] args)
{
      
    // I is the reference variable of Interface
    Interface I;
      
    // Initializing the I with objects of all the
    // classes one by one and calling the method greet()
    I = new Class1();
    I.greet();
  
    I = new Class2();
    I.greet();
      
    I = new Class3();
    I.greet();
    
    I = new Class4();
    I.greet();
      
    I = new Class5();
    I.greet();
}
}


输出
Welcome Geeks
Hello Geeks
Bella Ciao Geeks

示例 2:

C#

// C# program to illustrate the above concept
using System;
  
// Interface
interface Interface
{
      
    // Declaring Method 
    void greet();
}
  
// Creating classes and define the method
// greet() in all classes.
class Class1 : Interface
{
    public void greet()
    {
        Console.WriteLine("Welcome Geeks");
    }    
}
  
class Class2 : Interface
{
    public void greet()
    {
        Console.WriteLine("Hello Geeks");
    }    
}
  
class Class3 : Interface
{
    public void greet()
    {
        Console.WriteLine("Bella Ciao Geeks");
    }    
}
  
class Class4 : Interface
{
    public void greet()
    {
        Console.WriteLine("hola geeks");
    }    
}
  
class Class5 : Interface
{
    public void greet()
    {
        Console.WriteLine("welcome to geeksforgeeks");
    }
}
  
class GFG{
      
public static void Main(String[] args)
{
      
    // I is the reference variable of Interface
    Interface I;
      
    // Initializing the I with objects of all the
    // classes one by one and calling the method greet()
    I = new Class1();
    I.greet();
  
    I = new Class2();
    I.greet();
      
    I = new Class3();
    I.greet();
    
    I = new Class4();
    I.greet();
      
    I = new Class5();
    I.greet();
}
}
输出
Welcome Geeks
Hello Geeks
Bella Ciao Geeks
hola geeks
welcome to geeksforgeeks