为什么您必须关心对象引用是接口还是类? [英] Why should you ever have to care whether an object reference is an interface or a class?

查看:95
本文介绍了为什么您必须关心对象引用是接口还是类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎经常碰到讨论是否使用某种前缀/接口类型名称的后缀约定,通常在名称的开头添加"I".

I often seem to run into the discussion of whether or not to apply some sort of prefix/suffix convention to interface type names, typically adding "I" to the beginning of the name.

我个人在阵营中主张不加前缀,但这不是这个问题的目的.相反,这是我在讨论中经常听到的一种论点:

Personally I'm in the camp that advocates no prefix, but that's not what this question is about. Rather, it's about one of the arguments I often hear in that discussion:

您不再可以一目了然 无论是接口还是接口 课.

You can no longer see at-a-glance whether something is an interface or a class.

立即浮现在脑海中的问题是:除了对象创建之外,您为什么还要关心对象引用是类还是接口?

The question that immediately pops up in my head is: apart from object creation, why should you ever have to care whether an object reference is a class or an interface?

我已将此问题标记为与语言无关,但正如所指出的那样,可能并非如此.我认为这是因为尽管特定的语言实现细节可能很有趣,但我还是希望将其保留在概念上.换句话说,我认为,从概念上讲,您不必关心对象引用是键入为类还是接口,但是我不必确定 ,因此就是问题.

I've tagged this question as language agnostic, but as has been pointed out it may not be. I contend that it is because while specific language implementation details may be interesting, I'd like to keep this on a conceptual level. In other words, I think that, conceptually, you'd never have to care whether an object reference is typed as a class or an interface but I'm not sure, hence the question.

这不是关于IDE及其在可视化不同类型时的作用的讨论;浏览代码(包/源/任何形式)时,一定要注意对象的类型.也没有讨论有关命名约定的利弊.除了对象创建之外,我似乎无法弄清楚在哪种情况下,您实际上是否在乎是否引用了具体的类型或接口.

This is not a discussion about IDEs and what they do or don't do when visualizing the different types; caring about the type of an object is certainly a necessity when browsing through code (packages/sources/whatever form). Nor is it a discussion about the pros or cons about either naming convention. I just can't seem to figure out in what scenario, other than object creation, you actually care about wether or not you're referencing a concrete type or an interface.

推荐答案

在大多数情况下,您可能不在乎.但是在某些情况下,我可以想到您会想到的地方.有几种,并且确实会因语言而有所不同.有些语言不那么重视其他语言.

Most of the time, you probably don't care. But here are some instances that I can think of where you would. There are several, and it does vary a little bit by language. Some languages don't mind as much as others.

在控制反转的情况下(有人向您传递参数),就调用方法而言,您可能根本不在乎它是接口还是对象.但是在处理类型时,它肯定可以使差异.

In the case of inversion of control (where someone PASSES you a parameter) you probably don't care if it's an interface or an object as far as calling its methods etc. But when dealing with types, it definitely can make a difference.

  • 在诸如.NET语言之类的托管语言中,接口通常只能继承一个接口,而一个类可以继承一个类但实现许多接口.类与接口的顺序在类或接口声明中也可能很重要.因此,在定义新的类或接口时,您需要知道哪个是哪个.

  • In managed languages such as .NET languages, interfaces can usually only inherit one interface, whereas a class can inherit one class but implement many interfaces. The order of classes vs interfaces may also matter in a class or interface declaration. So you need to know which is which when defining a new class or interface.

在Delphi/VCL中,对接口进行引用计数并自动进行收集,而必须显式释放类,因此,整个生命周期管理将受到影响,而不仅是创建过程.

In Delphi / VCL, interfaces are reference counted and automatically collected, whereas classes must be explicitly freed, so lifecyle management on the whole is affected, not just the creation.

接口可能不是类引用的可行来源.

Interfaces may not be viable sources for class references.

接口可以强制转换为兼容的接口,但是在许多语言中,它们不能强制转换为兼容的类.可以将类强制转换为任何一个.

Interfaces can be cast to compatible interfaces, but in many languages, they cannot be cast to compatible classes. Classes can be cast to either.

接口可以传递给IID或IUnknown类型的参数,而类则不能(没有强制转换和支持接口).

Interfaces may be passed to parameters of type IID, or IUnknown, whereas classes cannot (without a cast and a supporting interface).

一个接口的实现是未知的.定义了它的输入和输出,但是抽象了创建输出的实现.通常,人们的态度可能是与班级一起工作时,人们可能知道班级的工作方式.但是,在使用接口时,不应进行此类假设.在一个完美的世界中,这可能没有任何区别.但实际上,这无疑会影响您的设计.

An interface's implementation is unknown. Its input and output are defined, but the implementation which creates the output is abstracted. In general, ones attitude may be that when working with a class, one may know how the class works. But when working with an interface, no such assumption should be made. In a perfect world, it might make no difference. But in reality, this most certainly can have affect your design.

这篇关于为什么您必须关心对象引用是接口还是类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆