NOOB:有人可以用外行的方式解释接口的原因吗? [英] NOOB: Could someone explain in laymen's terms the reason for an Interface?

查看:72
本文介绍了NOOB:有人可以用外行的方式解释接口的原因吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能只是愚蠢,但我不明白使用

界面的意义。

我目前正在阅读几本书我试着用vb.net教自己b $ b。只是为了它,你知道。 Kinda

有趣的东西。安美居;这两个都谈论了什么界面

,给出一个例子,然后愉快地继续他们的快乐方式离开我

来猜测你为什么创建和使用一个?


怎么样;


公共接口IJunk

Sub MoreJunk()

结束界面


公共类UseJunk

实现IJunk

Public Sub MoreJunk()实现Ijunk.MoreJunk

结束班级

任何不同于Just;


Public Class UseJunk

Public Sub MoreJunk()

结束课


????


除了更多打字之外。

I may be just stupid, but I don''t understand the point of using an
Interface.
I am going through a couple of books at the moment trying to teach
myself a little vb.net. Just for the heck of it, ya know. Kinda
interesting stuff. Anyhoo; Both of these talk about what an interface
is, give an example, and then happily go on their merry way leaving me
to guess as to WHY you would create and use one?

How is;

Public Interface IJunk
Sub MoreJunk()
End Interface

Public Class UseJunk
Implements IJunk
Public Sub MoreJunk() Implements Ijunk.MoreJunk
End Class
Any different than Just;

Public Class UseJunk
Public Sub MoreJunk()
End Class

????

Other than more typing that is.

推荐答案



这里有一种情况,我刚刚使用了一个界面(字面意思就像10分钟前一样)。


我正在将代码从VB6迁移到VB.NET,代码构建了一个

学区名称的树。这棵树通常只有一层,但是有时它是我们分区的两个级别。旧的VB6代码

基本上看起来像这样;


每个区

...如果分组那么

...... ..加入群组

...其他

......直接添加到root

。 ..结束如果

next


但这是一个cpu的waiste,因为if在循环中。为了解决这个问题,我在.NET中使用了一个接口(我也可以在VB6中完成,

但我还没读过GOF)。


VB.NET代码:


暗淡填充为IFiller

如果分组则

。 .. filler = new GroupedFiller()

else

... filler = new PlainFiller()

结束如果


每个区域

... filler.Add(区)

next


我有


界面IFiller

类GroupedFiller实现IFiller

类PlainFiller实现IFiller


这样,无论实际的实例是否为B
GroupedFiller或PlainFiller中的一个,上面的代码都可以作用于IFiller的实例




HTH,


Sam

星期五,2005年1月7日13:34:33 -0600,你< yo * @ me.com>写道:

Here''s one situation where I just used an interface (literally like 10
minutes ago).

I''m migrating code from VB6 to VB.NET and the code builds a tree of
school district names. The tree is usually just one level but
sometimes it''s two levels where we group districts. The old VB6 code
looked basically like this;

for each district
... if grouped then
... .. add to group
... else
... .. add directly to root
... end if
next

but this is a waiste of cpu since the if is inside the loop. To solve
this in .NET I used an interface (which I could have done in VB6 too,
but I hadn''t read GOF then).

VB.NET code:

dim filler as IFiller
if grouped then
... filler = new GroupedFiller()
else
... filler = new PlainFiller()
end if

for each district
... filler.Add(district)
next

and I have

interface IFiller
class GroupedFiller implements IFiller
class PlainFiller implements IFiller

This way, the code code above can act on an instance of IFiller the
same way regardless of whether the actual instance is one of
GroupedFiller or PlainFiller.

HTH,

Sam
On Fri, 7 Jan 2005 13:34:33 -0600, you <yo*@me.com> wrote:
我可能只是愚蠢,但我不明白使用
界面的意义。

我正在经历目前有几本书试图教自己一点点vb.net。只是为了它,你知道。有点儿有趣的东西。安美居;这两个都讨论了界面是什么,给出一个例子,然后愉快地继续他们的快乐方式离开我
猜测你为什么要创建和使用它?
I may be just stupid, but I don''t understand the point of using an
Interface.
I am going through a couple of books at the moment trying to teach
myself a little vb.net. Just for the heck of it, ya know. Kinda
interesting stuff. Anyhoo; Both of these talk about what an interface
is, give an example, and then happily go on their merry way leaving me
to guess as to WHY you would create and use one?



"这是接口提供的:一种继承只是一组方法和属性的方法

规范没有实现担心也没问题根据需要继承许多接口。

http://msdn.microsoft.com/library/de...interinher.asp

你 < yo*@me.com>在消息新闻中写道:MP ************************ @ news.microsoft.com ...
"This is what interfaces provide: a way to inherit just a set of method and property
specifications with no implementation to worry about and no problem inheriting from as
many interfaces as you need."

http://msdn.microsoft.com/library/de...interinher.asp
"you" <yo*@me.com> wrote in message news:MP************************@news.microsoft.com ...
我可能是只是愚蠢,但我不明白使用
界面的意义。

我现在正在阅读几本书,试图自己教一点点vb.net。只是为了它,你知道。有点儿有趣的东西。安美居;这两个都谈论了什么是一个界面,给出一个例子,然后愉快地继续他们的快乐方式离开我
猜测你为什么要创建和使用它?

如何;

公共接口IJunk
Sub MoreJunk()
结束接口

公共类UseJunk
实现IJunk Public Sub MoreJunk()实现Ijunk.MoreJunk
结束类

任何不同于Just;

Public Class UseJunk
Public Sub MoreJunk()<结束课程

????

除了更多打字之外。
I may be just stupid, but I don''t understand the point of using an
Interface.
I am going through a couple of books at the moment trying to teach
myself a little vb.net. Just for the heck of it, ya know. Kinda
interesting stuff. Anyhoo; Both of these talk about what an interface
is, give an example, and then happily go on their merry way leaving me
to guess as to WHY you would create and use one?

How is;

Public Interface IJunk
Sub MoreJunk()
End Interface

Public Class UseJunk
Implements IJunk
Public Sub MoreJunk() Implements Ijunk.MoreJunk
End Class
Any different than Just;

Public Class UseJunk
Public Sub MoreJunk()
End Class

????

Other than more typing that is.



将接口视为类模板。


它允许您确保所有使用该接口的类以一致的方式创建

。 br />

你用一个接口表明该类的行为是以特定的

方式,但与继承不同,该类本身将提供

实施。

查看ICloneable,IComparable,IDisposable接口,帮助

一些很好的实际使用。


" ;你" < yo*@me.com>在消息中写道

新闻:MP ************************ @ news.microsoft.com ...
think of an interface as a class template.

it allows you to make sure all classes that use the interface are created
in a consistent fashion.

you are indicating with an interface that the class behave in a particular
way, but unlike inheritance, the class itself will provide the
implementation.

check out the ICloneable, IComparable, IDisposable interfaces in help for
some good real world uses.

"you" <yo*@me.com> wrote in message
news:MP************************@news.microsoft.com ...
我可能只是愚蠢,但我不明白使用
界面的意义。

我目前正在阅读几本书自学了一点vb.net。只是为了它,你知道。有点儿有趣的东西。安美居;这两个都谈论了什么是一个界面,给出一个例子,然后愉快地继续他们的快乐方式离开我
猜测你为什么要创建和使用它?

如何;

公共接口IJunk
Sub MoreJunk()
结束接口

公共类UseJunk
实现IJunk Public Sub MoreJunk()实现Ijunk.MoreJunk
结束类

任何不同于Just;

Public Class UseJunk
Public Sub MoreJunk()<结束课程



除了更多打字之外。
I may be just stupid, but I don''t understand the point of using an
Interface.
I am going through a couple of books at the moment trying to teach
myself a little vb.net. Just for the heck of it, ya know. Kinda
interesting stuff. Anyhoo; Both of these talk about what an interface
is, give an example, and then happily go on their merry way leaving me
to guess as to WHY you would create and use one?

How is;

Public Interface IJunk
Sub MoreJunk()
End Interface

Public Class UseJunk
Implements IJunk
Public Sub MoreJunk() Implements Ijunk.MoreJunk
End Class
Any different than Just;

Public Class UseJunk
Public Sub MoreJunk()
End Class

????

Other than more typing that is.



这篇关于NOOB:有人可以用外行的方式解释接口的原因吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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