什么是 C# 中的接口以及在何处使用它

本文详细介绍了C#中的接口概念,强调接口是类与类之间的契约,仅包含方法定义而不实现。类必须为继承的接口提供实现,且一个类可以实现多个接口。文章通过多个例子展示了接口的使用,包括显式接口实现,并提到了接口在现实世界中的应用,如项目规划和体育比赛中的角色分配。

什么是 C# 中的接口以及在何处使用它

简介

在本文中,将讨论 C# 中的接口,即什么是接口,何时、何地以及为何使用接口。

接口与类非常相似。通过使用 interface 关键字,我们可以创建接口。接口是类和接口之间的契约;在接口中,我们可以放置方法定义,但不能放置声明。如果我们尝试提供声明,那么它将产生编译时错误。实际上界面是构建规划器。

如何?

因为在接口中我们只声明任务而不是声明。意味着接口是老板,与老板关联的所有员工都是类,如果老板分配任务,那么所有员工都有责任定义该任务。

例子

namespace ConsoleApplication
{
    class Program
    {
        interface ICustomer
        {
            void Print() //It will give error: Interface members can’t have definition
            {
                Console.WriteLine("welcome");
            }
        }      
        }
        class MyProgram
        {
            static void Main(string[] args)
            {         
            }
       }
}

问题:我们可以在接口中放置整数、字符串、双精度等字段吗?
答:不,不能声明,因为接口不能包含字段。

例子

namespace ConsoleApplication
{
    class Program
    {
        interface ICustomer
        {
            int i; // Interface can’t declare fields.
            void Print();           
        }      
        }
        class MyProgram
        {
            static void Main(string[] args)
            {            
            }
       }
}

就像类接口也包含属性、方法、委托或事件,但只是声明而不是实现。

如果一个类或结构继承自一个接口,它必须为所有接口成员提供实现。否则,我们会得到一个编译器错误。继承类负责定义所有方法。

例子

namespace ConsoleApplication
{
    class Program
    {
        interface ICustomer
        {           
            void Print();           
        }
        class Customer : ICustomer// Implementing Interface
        {
class Customer : ICustomer // Implementing Interface
            {
                public void Print();    // Error- Two Interfaces having same 
                                       //Method  Name.Program.Customer.Print()
                                       // must declare a body because it is not
                                      // marked abstract, extern, or partial               
            }      
        } 
     }
        class MyProgram
        {
            static void Main(string[] args)
            {            
            }
       }
}

默认情况下,所有成员都是公共的,并且它们不允许显式访问修饰符。

例子

class Program
        {
             interface ICustomer
            {
             public void Print(); // Error- The modifier 'public' is not
// valid for this item
            }
            class Customer : ICustomer// Implementing Interface
            {
                public void Print()// declaring that function
                {
                    Console.WriteLine("Print interface");
                }
            }
        }

一个类可以同时实现多个接口,但是一个类不能同时继承多个类。

例子

namespace ConsoleApplication
{
    class Program
    {
        interface I1
        {
            void I1Method();
        }
        interface I2
        {
            void I2Method();
        }
        class Customer : I1,I2  // Implemented more than one Interfaces
        {
            public void I1Method()
            {
                Console.WriteLine("I1Method Print");
            }
            public void I2Method()
            {
                Console.WriteLine("I2Method Print");
            }
        }
    class MyProgram
    {
        static void Main(string[] args)
        {
        }
    }
}
}

现在如果我们实现多个类那么

例子

namespace ConsoleApplication
{
    class Program
    {
        public class SampleClass1
        {
        }
        public class SampleClass2
        {
        }
        class Customer : SampleClass1, SampleClass2 // cannot have multiple base classes
        {
        }
    class MyProgram
    {
        static void Main(string[] args)
        {
        }
    }
}
}

接口可以继承自其他接口。继承该接口的类必须为整个接口继承链中的所有接口成员提供实现。

例子

namespace ConsoleApplication
{
    class Program
    {
        interface I1
        {
            void I1Method();
        }
        interface I2 : I1
        {
            void I2Method();
        }
        class Customer : I2  // Implementing  I2  Interface
        {
            public void I1Method() //Customer' does not implemented
                                    //interface member 'Program.I2. I2Method()
            {
                Console.WriteLine("I1Method Print");
            }
        }
    class MyProgram
    {
        static void Main(string[] args)
        {
        }
    }
}
}

I2继承自I1和I1接口,Customer类继承自I2 所以Customer类负责定义I1和I2继承链的所有成员。
我们无法创建接口实例,因为接口不完整。它包含方法签名但不包含声明,但是接口引用变量可以指向派生类对象,因为我们知道在继承链中基类引用变量可以指向派生类对象,同样它也适用于接口。

显式接口实现

如果一个类实现了两个包含具有相同签名的成员的接口,那么在该类上实现该成员将导致两个接口都使用该成员作为它们的实现。然后你可以像这样调用。
当我们有两个接口 I1 和 I2 具有相同的接口方法() 时,客户类同时实现 I1 和 I2 接口,然后我们如何调用 I1 的方法和 I2 的方法。

例子

namespace ConsoleApplication
{
    class Program
    {
        interface I1
        {
            void InterfaceMethod();
        }
        interface I2
        {
            void InterfaceMethod();
        }
    
class Customer : I1, I2
{
void I1.InterfaceMethod() //Explicitly method call
{
  Console.WriteLine("I1 Method Print");
}
void I2.InterfaceMethod() //Explicitly method call
{
  Console.WriteLine("I2 Method Print");
}

class MyProgram
{ 
static void Main(string[] args)
{
Customer c1 = new Customer();
((I1)c1).InterfaceMethod();
((I2)c1).InterfaceMethod();

//OR you can use like this
 I1 objI1 = new Customer();
 I2 objI2 = new Customer();

 objI1.InterfaceMethod();
 objI2.InterfaceMethod();
}
}
    }
}

注意

当类显式实现接口成员时,接口成员不能再通过类引用变量访问,只能通过接口引用变量访问。
显式实现的接口成员不允许使用访问修饰符

在哪里使用界面:
假设您是一名建筑规划师,并且您已经规划了一个项目。然后你应该创建一个接口并在那里声明你所有的方法签名。现在派生类可以自由地以自己的风格定义该功能或任务。

现实生活中的例子:
IPL 比赛是一个界面,IPL 购买的所有球员都是班级。现在 IPL 已经为所有玩家定义了一些任务,如 Playing()、Batting()、Bowling()、Fielding()、Keeping() 等。
现在所有玩家都有责任以自己的方式定义该任务,因为他们已经与 Interface (Means with IPL) 签订了合同。他们不能否认实施。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值