接口 [英] Interfaces

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

问题描述

为什么代码派生自例如IComparer


ie


class SomeClass:IComparer

{

public SomeClass()

{

}


public int Compare(对象x,对象y)

{

返回someInt;

}

}

它只是使用它而且可以在没有得到ICompare的情况下完成。

得到相同的结果。


class SomeClass

{

public SomeClass()

{

}

public int Compare(object x,object y)

{

返回someInt;

}

}


-


Duncan McNutt

微软产品停用团队

-

解决方案

另一个问题...


因为SomeClass派生自IComparer接口

如果我使用SomeClass作为另一个基地... ...
>
class SomeNewClass:SomeClass

{

public SomeNewClass()

{

}

}


由于没有比较方法而导致上述失败,因为SomeClass派生自

ICompare并且它必须实现Compare .. like ..


class SomeNewClass:SomeClass

{

public SomeNewClass()

{

}


public int比较(对象x,对象y)

{

返回someInt; < br $>
}

}


以上都可行。


我明白这个正确吗?


-


Duncan McNutt

微软产品停用团队

-

" Duncan McNutt。[FTSE]" < PI ******* @ 127.0.0.701>在消息中写道

news:uL ************** @ TK2MSFTNGP09.phx.gbl ...

为什么代码来自例如,IComparer



类SomeClass:IComparer
{
public SomeClass()
{
}

public int比较(对象x,对象y)
{
返回someInt;
}
}

当它只是使用它并且可以在没有得到ICompare
的情况下完成并获得相同的结果。

类SomeClass
{
public SomeClass()
{
}

public int比较(对象x,对象y)
{
返回someInt;
}
}

-

Duncan McNutt
微软产品停用团队
-



你好Duncan,


你不是从界面派生的,你实现它,有一个不同,

一个接口就像一个模板,它没有实现它只保证

实现它的类有一个预定义的setof方法(

接口),当你派生自一个扩展该类的类,添加新的

功能。你没有用界面做到这一点。


希望这个帮助,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局

" Duncan McNutt。[FTSE]" < PI ******* @ 127.0.0.701>在消息中写道

news:uL ************** @ TK2MSFTNGP09.phx.gbl ...

为什么代码来自例如,IComparer



类SomeClass:IComparer
{
public SomeClass()
{
}

public int比较(对象x,对象y)
{
返回someInt;
}
}

当它只是使用它并且可以在没有得到ICompare
的情况下完成并获得相同的结果。

类SomeClass
{
public SomeClass()
{
}

public int比较(对象x,对象y)
{
返回someInt;
}
}

-

Duncan McNutt
微软产品停用团队
-



哦好吧是啊oops:D不思考*得到咖啡*


所以为什么从ICompare继承它的同时易于实施比较

无论如何及其派生。


-


Duncan McNutt

Microsoft产品停用团队

-

" Peter Vidler" < PV ***** @ gawab.com>在消息中写道

news:bj **************** @ newsfep1-win.server.ntli.net ...


上面会因为没有比较方法而失败,因为SomeClass是从ICompare派生出来的,它必须实现比较......就像..
不,因为SomeNewClass将从SomeClass继承实现。



我几乎可以肯定它;)

Pete



Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare and
got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--

解决方案

And another question...

Because SomeClass derives from the IComparer interface
if i use SomeClass as a base of another like...

class SomeNewClass : SomeClass
{
public SomeNewClass()
{
}
}

Would that above fail due to no Compare method as SomeClass is derived from
ICompare and it MUST implement Compare.. like..

class SomeNewClass : SomeClass
{
public SomeNewClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

The above would work.

Do i understand this correctly?

--

Duncan McNutt
Microsoft Product Deactivation Team
--
" Duncan McNutt .[FTSE]" <pi*******@127.0.0.701> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...

Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare and got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--



Hi Duncan,

You do not derive from an interface, you implement it, there is a diference,
an interface is like a template, it has no implementation it only assure
that the classes that implement it have a predefined setof methods ( the
interface ) , when you derive from a class you extend that class, add new
functionality. you do not do that with an interface.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
" Duncan McNutt .[FTSE]" <pi*******@127.0.0.701> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...

Why does code derive from for example, IComparer

ie,

class SomeClass : IComparer
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}
When it just uses that and could have done without deriving ICompare and got the same result.

class SomeClass
{
public SomeClass()
{
}

public int Compare(object x, object y)
{
return someInt;
}
}

--

Duncan McNutt
Microsoft Product Deactivation Team
--



oh well yeah oops :D wasnt thinking *gets coffee*

So why inherit from ICompare when its just as easy to implement Compare
anyway and its derived.

--

Duncan McNutt
Microsoft Product Deactivation Team
--
"Peter Vidler" <pv*****@gawab.com> wrote in message
news:bj****************@newsfep1-win.server.ntli.net...

Hi,

Would that above fail due to no Compare method as SomeClass is
derived from ICompare and it MUST implement Compare.. like..
No, because SomeNewClass will inherit the implementation from SomeClass.


I''m almost certain of it ;)

Pete



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

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