C#接口 - 这有什么意义?这有、接口、意义

2023-09-02 10:17:47 作者:你、乱了我的夏天

我是一个Python的背景,我试图去掌握C#,因为一对夫妇的家伙和我决定做一个项目,并选择了C#吧(不要担心回合的原因)。

原因接口真正躲开我。据我了解,这是怎样的一个变通方法不存在多继承这并不在C#中存在(或所以我告诉)的。

但是,所有我看到的是,您predefine一些成员和功能,你就必须重新定义在类了。从而使界面多余的,因为你们都做一遍,所以没有真正的需要在我的眼里,以pre-定义它。它只是感觉像语法......嗯,垃圾给我(请没有冒犯的意思。垃圾在无用的东西)。

在下面给出从堆栈溢出不同的C#接口螺纹采取的例子中,我只想创建一个名为而不是一个接口比萨的基类。

我会很感激,如果有人可以提供一些线索这一点,因为它确实是没有意义的我,在所有:(

多谢了。

简单的例子(从不同的堆栈溢出的贡献取)

 公共接口IPizza
{
    公共无效订单();

}

公共类PepperoniPizza:IPizza
{
    公共无效订单()
    {
        //订购香肠比萨
    }
}

公共类HawaiiPizza:IPizza
{
    公共无效订单()
    {
        //订单HawaiiPizza
    }
}
 

解决方案

的一点是,界面再presents一个的合同的。一组公开方法的任何实现类都必须有。从技术上讲,接口只规管的语法,即什么方法有哪些,哪些参数他们得到和他们返回。通常它们封装语义好,虽然这只有文档。

c 连接数据库

您可以再有一个接口的不同实现和交换出来的意愿。在你的榜样,因为每一个比萨饼实例是一个 IPizza 您可以使用 IPizza 无论你处理一个未知的比萨饼的实例类型。其类型从 IPizza 继承是保证任何实例可订购,因为它有一个订单()方法。

Python的不是静态类型,因此类型保存,并期待在运行时。所以,你可以尝试调用一个订单()上的任何对象的方法。运行时是幸福的,只要对象有这样一种方法,也许只是耸耸肩,说»咩。«如果没有。事实并非如此在C#。编译器负责制定正确的电话,如果只是只是一些随机的对象编译器不知道还运行在该实例是否有方法。但从编译器的角度来看它是无效的,因为他无法验证它。 (你可以做反射或动态关键字这样的事情,但是这是怎么回事有点远,现在,我想。)

另外请注意,通常意义上的接口并不一定是一个C#接口,它可能是一个抽象类,以及甚至正常类(可以派上用场,如果所有子类需要有一些共同的code - 在大多数情况下,然而,接口就足够了)

I am from a python background and I am trying to get to grips with C#, because a couple of guys and I decided to do a project and chose C# for it (don't worry bout the reasons).

The reason for interfaces truly eludes me. From what I understand, it is kind of a work around for the non-existent multi-inheritance which doesn't exist in C# (or so I was told).

But all I see is, that you predefine some members and functions, which you then have to re-define in the class again. Thus making the interface redundant because you do it all over again, so no real need in my eyes to pre-define it. It just feels like syntactic... well, junk to me (Please no offense meant. Junk as in useless stuff).

In the example given below taken from a different C# interfaces thread on stack overflow, I would just create a base class called Pizza instead of an interface.

I would be very grateful, if someone could shed some light on this, because it really makes no sense to me, at all :(

Thanks a lot.

easy example (taken from a different stack overflow contribution)

public interface IPizza
{
    public void Order();

}

public class PepperoniPizza : IPizza
{
    public void Order()
    {
        //Order Pepperoni pizza
    }
}

public class HawaiiPizza : IPizza
{
    public void Order()
    {
        //Order HawaiiPizza
    }
}

解决方案

The point is that the interface represents a contract. A set of public methods any implementing class has to have. Technically, the interface only governs syntax, i.e. what methods are there, what arguments they get and what they return. Usually they encapsulate semantics as well, although that only by documentation.

You can then have different implementations of an interface and swap them out at will. In your example, since every pizza instance is an IPizza you can use IPizza wherever you handle an instance of an unknown pizza type. Any instance whose type inherits from IPizza is guaranteed to be orderable, as it has an Order() method.

Python is not statically-typed, therefore types are kept and looked up at runtime. So you can try calling an Order() method on any object. The runtime is happy as long as the object has such a method and probably just shrugs and says »Meh.« if it doesn't. Not so in C#. The compiler is responsible for making the correct calls and if just have just some random object the compiler doesn't know yet whether the instance during runtime will have that method. From the compiler's point of view it's invalid since he cannot verify it. (You can do such things with reflection or the dynamic keyword, but that's going a bit far right now, I guess.)

Also note that an interface in the usual sense does not necessarily have to be a C# interface, it could be an abstract class as well or even a normal class (which can come in handy if all subclasses need to share some common code – in most cases, however, interface suffices).