为什么'接口'? [英] Why 'Interfaces'?

查看:61
本文介绍了为什么'接口'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在C#中构建一个类,我可以声明一个方法摘要。

如果有人派生自这个类,那么抽象方法

必须覆盖计算成功。


ISTM接口提供类似的服务,即

所有方法都必须实现才能进行计算

成功。


为什么要使用其中一个?或者两者兼而有之?

是因为接口可以用_any_

语言实现吗?


-

Grace + Peace,

Peter N Roth

Engineering Objects International
http://engineeringobjects.com

If i build a class in C#, i can declare a method abstract.
If someone derives from this class, the abstract method
must be overriden for the computation to succeed.

ISTM that an Interface provides a similar service, i.e.,
all methods must be implemented in order for the computation
to succeed.

Why use one or the other? Or maybe both?
Is it because interfaces can be implemented in _any_
language?

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com

推荐答案

继承遵循类层次结构。接口是一个合同,

描述了应该包含在实现

接口的类中的成员。接口跨类层次结构边界。另外,一个类只能从一个超类继承
,而一个类可以实现几个

接口。


希望总和它了。


干杯,

Wim Hollebrandse
http://www.wimdows.net
http://www.wimdows.com

" Peter N Roth" <再*********** @ mycompany.com>在消息中写道

新闻:#O ************** @ TK2MSFTNGP11.phx.gbl ...
Inheritance follows the class hierarchies. And interface is a contract which
describes what members should be included in the class which implements the
interface. Interfaces cross class hierarchy boundaries. Also, a class can
only inherit from one superclass, whereas a class can implement several
interfaces.

Hope that sums it up.

Cheers,
Wim Hollebrandse
http://www.wimdows.net
http://www.wimdows.com
"Peter N Roth" <re***********@mycompany.com> wrote in message
news:#O**************@TK2MSFTNGP11.phx.gbl...
如果我建立一个在C#中,我可以声明一个方法摘要。
如果有人派生自这个类,那么抽象方法必须覆盖才能使计算成功。

ISTM即一个接口提供类似的服务,即
必须实施所有方法才能使计算成功。

为什么要使用其中一种?或者两者兼而有之?
是因为界面可以用_any_
语言实现吗?

-
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
If i build a class in C#, i can declare a method abstract.
If someone derives from this class, the abstract method
must be overriden for the computation to succeed.

ISTM that an Interface provides a similar service, i.e.,
all methods must be implemented in order for the computation
to succeed.

Why use one or the other? Or maybe both?
Is it because interfaces can be implemented in _any_
language?

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com



C#没有多重继承,但它有多个接口,


有多个接口和有什么区别多个基础

类关于多重继承所带来的问题

声称为什么我们在C#中没有多重继承。

" Wim Hollebrandse" < wimATwimdows.com>在消息中写道

news:#s ************* @ TK2MSFTNGP10.phx.gbl ...
C# has no multiple inheritance, yet it has multiple interfaces,

What is the difference between having multiple interfaces and multiple base
classes with regard to problems assosicated with multiple inheritance that
is claimed why we have no multiple inheritance in C#.
"Wim Hollebrandse" <wimATwimdows.com> wrote in message
news:#s*************@TK2MSFTNGP10.phx.gbl...
继承遵循类层次结构。接口是一个合约
,它描述了在接口中实现
的类中应该包含哪些成员。接口跨类层次结构边界。此外,一个类只能从一个超类继承,而一个类可以实现几个
接口。

希望总结一下。

干杯,
Wim Hollebrandse
http://www.wimdows.net
http://www.wimdows.com

Peter N Roth <再*********** @ mycompany.com>在消息中写道
新闻:#O ************** @ TK2MSFTNGP11.phx.gbl ...
Inheritance follows the class hierarchies. And interface is a contract which describes what members should be included in the class which implements the interface. Interfaces cross class hierarchy boundaries. Also, a class can
only inherit from one superclass, whereas a class can implement several
interfaces.

Hope that sums it up.

Cheers,
Wim Hollebrandse
http://www.wimdows.net
http://www.wimdows.com
"Peter N Roth" <re***********@mycompany.com> wrote in message
news:#O**************@TK2MSFTNGP11.phx.gbl...
如果我用C#构建一个类,我可以声明一个方法摘要。
如果某人派生自这个类,必须覆盖抽象方法
才能使计算成功。

ISTM接口提供类似的服务,即
必须实施所有方法才能使计算成功。

为什么要使用其中一种?或者两者兼而有之?
是因为界面可以用_any_
语言实现吗?

-
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
If i build a class in C#, i can declare a method abstract.
If someone derives from this class, the abstract method
must be overriden for the computation to succeed.

ISTM that an Interface provides a similar service, i.e.,
all methods must be implemented in order for the computation
to succeed.

Why use one or the other? Or maybe both?
Is it because interfaces can be implemented in _any_
language?

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com




抽象类包含一个或多个抽象成员。

因此,你可以设计一个90 %完成课程离开

只有一种方法未定义,并要求从中派生的非抽象的
类包括

的实现其余方法。由于.NET只允许单个

类的无效,因此您从

派生的抽象类将是您派生的唯一类。


另一方面,接口只定义成员

定义。实现接口的类必须实现该接口的每个成员,但是它完全取决于实现者是如何完成的。


任何实现接口的类都可以转换为

接口。因此,可以编写在界面上操作的方法

,而不必在设计时知道你将要操纵什么样的对象。


例如,这就是foreach语句在

C#中的工作方式。对于与foreach语句一起使用的类,它必须实现IEnumerable接口。 foreach使用

此界面中定义的方法来逐步查看集合中的

对象。显然,那个为b / b $ b编写了foreach方法的人并不知道你要去的所有关于BasketOfEggs类的内容是什么?

to写在将来,但如果你实现IEnumerable

(和IEnumerator)你可以写一个语句,如-

foreach(MyBasketOfEggs中的Egg egg)。没关系

Egg或BasketOfEggs派生的类。


最后(最后我的帖子,不是主题,这是

比我有资格谈论的话要广泛得多),

对一个类可以实现的接口数量没有限制。


Charlie
An abstract class contains one or more abstract members.
Therfore, you could design a 90% complete class leaving
only one method undefined and requiring that non-abstract
classes that derive from it include an implementation of
the remaining method. Since .NET allows only single
ineritence of classes, the abstract class you derive from
will be the only class you derive from.

Interfaces, on the other hand, only define member
definitions. Classes that implement the interface must
implement each member of that interface, but it is
completely up the implementor how it is done.

Any class that implements an interface can be cast to that
interface. Therefore, it is possible to write methods
that operate on an interface, without knowing at design
time what kind of object you''re going to be manipulating.

For example, this is how the foreach statement works in
C#. For a class to be used with the foreach statement, it
must implement the interface IEnumerable. foreach uses
the methods defined in this interface to step through the
objects in your collection. Obviously, the person who
wrote the foreach method for Microsoft didn''t know
anything about the BasketOfEggs class that you were going
to write in the future, but if you implement IEnumerable
(and IEnumerator) you can write a statement like -
foreach(Egg egg in MyBasketOfEggs). It does not matter
what class Egg or BasketOfEggs derives from.

Lastly (lastly for my post, not for the subject, which is
much more expansive than I''m qualified to talk about),
there is no limit to the number of interfaces a class can
implement.

Charlie
-----原始消息-----
如果我用C#构建一个类,我可以声明一个方法摘要。
如果某人派生自这个类,必须覆盖抽象方法
才能使计算成功。

ISTM接口提供类似的服务,即
必须实现所有方法才能使
计算成功。

为什么要使用其中一个?或者两者兼而有之?
是因为界面可以用_any_
语言实现吗?

-
Grace + Peace,
Peter N Roth http://engineeringobjects.com

.
-----Original Message-----
If i build a class in C#, i can declare a method abstract.
If someone derives from this class, the abstract method
must be overriden for the computation to succeed.

ISTM that an Interface provides a similar service, i.e.,
all methods must be implemented in order for the computationto succeed.

Why use one or the other? Or maybe both?
Is it because interfaces can be implemented in _any_
language?

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com

.



这篇关于为什么'接口'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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