适当的界面使用和说明(Proper interface use and explanation)

其他开发 IT屋
百度翻译此文   有道翻译此文
问 题

I have been gasping my head around interfaces for quite a long time and I am still unable to understand their function and what do they do. I read thousands of forum posts and I keep asking myself a single question. I will explain to you what the question I am asking is.

We have got this core, which does a really simple thing. We take one class, and in the main class we call the function declared in that class, by using an interface.

public interface IAnimal
{
   string GetDescription();
}

class Cat : IAnimal
{     
   public string GetDescription()
   {
      return "I'm a cat";
   }
}

class Program
{
    static void Main(string[] args)
    {
       Cat myCat = new Cat();

       Console.WriteLine(myCat.GetDescription());
    }
}

The question I keep asking myself. Why are we doing this? What is the point? Why not simply do this:

class Cat
{     
   public static string GetDescription()
   {
      return "I'm a cat";
   }
}

class Program
{
    static void Main(string[] args)
    {
    Cat.GetDescription();
    }
}

I would greatelly appreciate any help and a PROPER explanation. I am open for simple accurate examples. Nothing too complex. I would also like you to provide some text and explain why is this and that there.

Thank you.

EDIT: I am sorry for confusing some people. In the second description I forgot to change public void cat to public static void cat, therefore it was unaccessable and did not compile.

解决方案

This question is asked by many new programmers and you will get several answers for it. I am a Computer Science student and I will try my best to explain this to you.

Interface provides alternative to multiple inheritance. C# allows you to inherent from only one single base class but we can implement as many interfaces as we want.

The example you posted above has a class Cat which implements an interface IAnimal. This is little bit wrong in my opinion because you should use Animal as an abstract class. Pragmatically this will not make any difference butt the concept which you are trying to use here is of inheritance (A cat inherit from Animal). Following line is from MSD:

if the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

So now you might be thinking then what are the real purpose of interfaces and when to use them. Answer is simple, use interfaces when you want to impose some kind of restrictions to the users of your interface (or other developers who implements your interface) or want to extract common things from several classes.

A nice example for use of inheritance and interfaces will be:

Lets say you are building a software for electronics devices. And the scenario is like (parent class -> inherited class) : Gadgets -> Electronic Gadgets -> Telephony Gadgets -> Mobiles -> Smart Phones -> Tablest

And say Mobiles, Smart Phones and tablets have a common feature of FM-RADIO. This feature is available in other Gadgets which are not Telephony Gadgets. Now it will be perfect to use FM-Radio as an interface. Every gadget will provide their own definition of FM-Radio but all will share same functionality.

Hope I have cleared your confusion.

本文地址:IT屋 » Proper interface use and explanation

问 题

我一直在界面上喘气很长时间,我仍然无法理解他们的功能以及他们做了什么。我阅读了数以千计的论坛帖子,我一直在问自己一个问题。我会向你解释我要问的问题是什么。



我们有这个核心,这真的很简单。我们接受一个类,并在主类中通过使用接口调用在该类中声明的函数。



  public interface IAnimal 
{
string GetDescription();
}

class Cat:IAnimal
{
public string GetDescription()
{
return“我是一只猫”;
}
}

class程序
{
static void Main(string [] args)
{
Cat myCat =新猫();

Console.WriteLine(myCat.GetDescription());
}
}


我一直在问自己的问题。我们为什么要这样做呢?有什么意义?
为什么不简单地这样做:



  class Cat 
{
public static string GetDescription( )
{
返回“我是一只猫”;
}
}

class程序
{
static void Main(string [] args)
{
Cat.GetDescription ();
}
}


我会非常感谢任何帮助和正确的解释。我愿意接受简单准确的例子。没有什么太复杂的了。我还希望你提供一些文字并解释为什么会这样。那就是。



谢谢。



编辑:
我很抱歉让一些人感到困惑。在第二个描述中,我忘了将public void cat更改为public static void cat,因此它无法访问且无法编译。


解决方案

许多新程序员都会问这个问题,你会得到几个答案。我是计算机科学专业的学生,​​我会尽力向您解释。




接口提供了多重继承的替代方案。 C#允许你从一个基类中固有
,但是我们可以实现尽可能多的
接口。




上面发布的示例有一个类Cat,它实现了一个接口 IAnimal 。这在我看来有点不对,因为你应该使用 Animal 作为抽象类。实际上,这不会有任何区别,但是你在这里尝试使用的概念是继承(一只猫继承自Animal)。以下行来自 MSD




如果您创建的功能在各种不同的对象中都有用,请使用界面。抽象类应该主要用于密切相关的对象,而接口最适合为不相关的类提供通用功能。




所以现在你可能会想到接口的真正目的是什么以及何时使用它们。答案很简单,当你想对接口的用户(或实现你的接口的其他开发人员)施加某种限制或者想要从几个类中提取常见的东西时,使用接口。



使用继承和接口的一个很好的例子是:



假设您正在为电子设备构建软件。场景就像(父类 - >继承类):
小工具 - >电子小工具 - >电话小工具 - >移动电话 - >智能手机 - >桌面



并说移动设备,智能手机和平板电脑具有 FM-RADIO 的共同特征。其他小工具可以使用此功能,而不是电话小工具。现在,将FM-Radio用作接口将是完美的。每个小工具都会提供他们自己的FM-Radio定义,但所有小工具都会共享相同的功能。



希望我已经清除了你的困惑。


本文地址:IT屋 » 适当的界面使用和说明

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起
}