界面问题 [英] interface question

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

问题描述

大家好。我现在有关于接口的问题。根据这本书

我正在阅读,当你实现一个接口时,类或结构

必须声明接口包含的所有方法。


我的问题是,如果你必须声明每种方法(而不是简单地使用''b $ b''使用''这些方法,比如#included文件),那么为什么不呢?

自己声明方法,就像你自己创建它们一样?如果您必须手动声明

所有方法,那么使用界面有什么优惠呢?


谢谢。

Hi all. I have a question about interfaces now. According to the book
I''m reading, when you implement an interface, the class or structure
has to declare all the methods that the interface includes.

My question is, if you must declare each method (as opposed to simply
''using'' those methods like an #included file), then why not just
declare the methods on your own as if you created them yourself? What
advantage does using an interface have if you must manually declare
all the methods anyway?

Thanks.

推荐答案

>我的问题是,如果你必须声明每个方法(而不是简单的
> My question is, if you must declare each method (as opposed to simply
''使用''那些方法,如#included文件),那么为什么不只是
声明这些方法就像你自己创建它们一样?如果必须手动声明
所有方法,使用界面有什么
优势呢?
''using'' those methods like an #included file), then why not just
declare the methods on your own as if you created them yourself? What advantage does using an interface have if you must manually declare
all the methods anyway?




答案是多态和多态 :如果你声明一堆类b / b实现相同的接口,那么你可以编写代码

,可以对任何这些类进行操作,因为代码知道

它们都使用相同的调用实现相同的接口方法

序列(签名)。作为一个(愚蠢的)例子,如果我有一个

接口:


公共接口IGreeting

{

public string Hello {get; }

public void ShakeHands();

}


我写了几个课:


公共类英语:IGreeting

{

public string Hello {get {return" Hello" ;; } $

public void ShakeHands(){//在这里做一些事情}

}


公共类西班牙语:IGreeting

{

公共字符串Hello {get {return"Buenosdías" ;; } $

public void ShakeHands(){//在这里做一些事情}

}


然后我可以写一个方法在其他一些课程中:


public void SayHello(IGreeting person)

{

Console.WriteLine(person.Hello);

person.ShakeHands();

}


" SayHello"可以打招呼和握手,而不知道它是什么样的人,因为它知道传递给它的所有对象必须实现IGreeting界面,所以必须有a你好 string

property and aShakeHands"没有参数的方法。


没有界面,就没有办法表达,一个对象

有一个Hello字符串属性和一个ShakeHands无效的方法,

不带参数。


(顺便说一句,关于术语的挑剔细节:接口_declares_

属性,方法和事件。这个类要么_implements_他们

或_defines_他们(他们俩都是同样的东西,真的)。是的,

类也再次声明它们,但这并不像它实现它们的想法那么重要。



The answer is something called "polymorphism": if you declare a bunch
of classes as implementing the same interface, then you can write code
that can operate on any of those classes, because the code knows that
they all implement the same interface methods with the same calling
sequences (signatures). As a (stupid) example, if I were to have an
interface:

public interface IGreeting
{
public string Hello { get; }
public void ShakeHands();
}

and I write a couple of classes:

public class English : IGreeting
{
public string Hello { get { return "Hello"; } }
public void ShakeHands() { // Do some stuff here }
}

public class Spanish : IGreeting
{
public string Hello { get { return "Buenos días"; } }
public void ShakeHands() { // Do some stuff here }
}

then I could write a method in some other class:

public void SayHello(IGreeting person)
{
Console.WriteLine(person.Hello);
person.ShakeHands();
}

"SayHello" can say hello and shake hands without knowing what kind of
person it is, because it knows that all objects passed to it must
implement the IGreeting interface, and so must have a "Hello" string
property and a "ShakeHands" method that takes no arguments.

Without the interface, there would be no way to express, "An object
that has a Hello string property and a ShakeHands void method that
takes no arguments."

(By the way, a picky detail about terminology: the interface _declares_
the properties, methods, and events. The class either _implements_ them
or _defines_ them (they both mean the same thing, really). Yes, the
class also declares them again, but that''s not so important as the idea
that it implements them.)


这是一个关于如何利用接口的真实示例:

http://www.eggheadcafe.com/articles/20031103.asp


-

2004和2005 Microsoft MVP C#

Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

" John Salerno" <乔****** @ NOSPAMgmail.com>在消息中写道

新闻:EN ******************** @ rcn.net ...
Here is a real world example of how you might utilize interfaces:

http://www.eggheadcafe.com/articles/20031103.asp

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:EN********************@rcn.net...
你好所有。我现在有关于接口的问题。根据我正在阅读的书,当你实现一个界面时,类或结构必须声明界面包含的所有方法。

我的问题是,如果你必须声明每个方法(而不是简单地''使用''那些方法,如#included文件),那么为什么不只是自己声明方法,就像你自己创建它们一样?如果必须手动声明
所有方法,使用界面有什么好处?

谢谢。
Hi all. I have a question about interfaces now. According to the book
I''m reading, when you implement an interface, the class or structure
has to declare all the methods that the interface includes.

My question is, if you must declare each method (as opposed to simply
''using'' those methods like an #included file), then why not just
declare the methods on your own as if you created them yourself? What
advantage does using an interface have if you must manually declare
all the methods anyway?

Thanks.


谢谢布鲁斯。这对我来说是一个及时的主题。


SayHello方法签名包含IGreeting类型的person参数。

如果person参数是IGreeting类型的对象这个人在哪里获取它的字符串数据然后作为值传递给WriteLine方法的

person.Hello参数?


<%= Clinton Gallagher

" Bruce Wood" <峰; br ******* @ canada.com>在消息中写道

news:11 ********************* @ f14g2000cwb.googlegro ups.com ...
Thanks Bruce. This is a timely topic for me.

The SayHello method signature includes a person parameter of type IGreeting.
If the person parameter is an object of type IGreeting where does the person
object get its string data that is then passed through as a value to the
person.Hello parameter of the WriteLine method?

<%= Clinton Gallagher
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
我的问题是,如果你必须声明每个方法(而不是简单的
''使用''这些方法,如#included文件),那么为什么不只是
声明你自己的方法好像你自己创造了它们?如果必须手动声明
所有方法,使用界面有什么
优势呢?
My question is, if you must declare each method (as opposed to simply ''using'' those methods like an #included file), then why not just
declare the methods on your own as if you created them yourself? What advantage does using an interface have if you must manually declare
all the methods anyway?




答案是多态和多态 :如果你声明一堆类b / b实现相同的接口,那么你可以编写代码

,可以对任何这些类进行操作,因为代码知道

它们都使用相同的调用实现相同的接口方法

序列(签名)。作为一个(愚蠢的)例子,如果我有一个

接口:


公共接口IGreeting

{

public string Hello {get; }

public void ShakeHands();

}


我写了几个课:


公共类英语:IGreeting

{

public string Hello {get {return" Hello" ;; } $

public void ShakeHands(){//在这里做一些事情}

}


公共类西班牙语:IGreeting

{

公共字符串Hello {get {return"Buenosdías" ;; } $

public void ShakeHands(){//在这里做一些事情}

}


然后我可以写一个方法在其他一些课程中:


public void SayHello(IGreeting person)

{

Console.WriteLine(person.Hello);

person.ShakeHands();

}


" SayHello"可以打招呼和握手,而不知道它是什么样的人,因为它知道传递给它的所有对象必须实现IGreeting界面,所以必须有a你好 string

property and aShakeHands"没有参数的方法。


没有界面,就没有办法表达,一个对象

有一个Hello字符串属性和一个ShakeHands无效的方法,

不带参数。


(顺便说一句,关于术语的挑剔细节:接口_declares_

属性,方法和事件。这个类要么_implements_他们

或_defines_他们(他们俩都是同样的东西,真的)。是的,

类也再次声明它们,但这并不像它实现它们的想法那么重要。)



The answer is something called "polymorphism": if you declare a bunch
of classes as implementing the same interface, then you can write code
that can operate on any of those classes, because the code knows that
they all implement the same interface methods with the same calling
sequences (signatures). As a (stupid) example, if I were to have an
interface:

public interface IGreeting
{
public string Hello { get; }
public void ShakeHands();
}

and I write a couple of classes:

public class English : IGreeting
{
public string Hello { get { return "Hello"; } }
public void ShakeHands() { // Do some stuff here }
}

public class Spanish : IGreeting
{
public string Hello { get { return "Buenos días"; } }
public void ShakeHands() { // Do some stuff here }
}

then I could write a method in some other class:

public void SayHello(IGreeting person)
{
Console.WriteLine(person.Hello);
person.ShakeHands();
}

"SayHello" can say hello and shake hands without knowing what kind of
person it is, because it knows that all objects passed to it must
implement the IGreeting interface, and so must have a "Hello" string
property and a "ShakeHands" method that takes no arguments.

Without the interface, there would be no way to express, "An object
that has a Hello string property and a ShakeHands void method that
takes no arguments."

(By the way, a picky detail about terminology: the interface _declares_
the properties, methods, and events. The class either _implements_ them
or _defines_ them (they both mean the same thing, really). Yes, the
class also declares them again, but that''s not so important as the idea
that it implements them.)


这篇关于界面问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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