界面和枚举 [英] Interface and enums

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

问题描述



有人可以解释为什么你不能在界面中声明枚举吗?

编译器说接口不能声明类型

忽略语法含义在我看来,你应该能够

声明枚举存在。即它在界面的精神范围内,表示它存在。实施班将为会员提供。


,例如我有一个执行功能的代码库。

它暴露了两个接口,允许两种不同的方式要求

函数。

一种方法涉及公共枚举,即一些方法调用接受

枚举作为参数,另一种方法不接受。

在我看来,我应该能够在一个界面中声明枚举

并将其保留在另一个界面之外。

目前,枚举可用于任一接口的声明对象。

例如

ImyFirstInterface FirstInterface = new MyLib.myImplementingclass();

ImySecondInterface SecondInterface = new MyLib.myImplementingclass();


FirstInterface和SecondInterface都可以看到MyLib.myPublicEnum

谢谢

Bob


Hi,
Can someone explain why you can''t declare enums in an interface?
The compiler says "interfaces can''t declare types"
Ignoring the syntax implications it seems to me that you should be able to
declare that an enum exists. i.e. It is within the ''spirit'' of an interface
to state that it exists. The implementing class will provide the members.

e.g. I have a code library that performs a function.
It exposes two interfaces which allow for two different ways of asking for
the function.
One way involves a public enum, i.e. Some of the method calls accept the
enum as a parameter, the other does not.
It seems to me that I should be able to declare the enum in one interface
and leave it out of the other.
Currently the enum is available to declared objects of either interface.
e.g.
ImyFirstInterface FirstInterface = new MyLib.myImplementingclass();
ImySecondInterface SecondInterface = new MyLib.myImplementingclass();

Both FirstInterface and SecondInterface can see MyLib.myPublicEnum
thanks
Bob


推荐答案


>忽略语法含义在我看来你应该能够
d一个枚举存在的eclare。即,它在界面的精神内,表明它存在。实施班将提供成员。
>Ignoring the syntax implications it seems to me that you should be able to
declare that an enum exists. i.e. It is within the ''spirit'' of an interface
to state that it exists. The implementing class will provide the members.



如果你不知道它的
成员,你的客户代码会如何消耗enum?

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。

How would your client code consume the enum if you don''t know its
members?
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


嗨Mattias,

我发现它与其他任何界面的发生方式大致相同

实现。

ie


****接口代码*****

公共接口IMyInterface

{

enum MyJamChoice {};

bool JamInStock(MyJamChoice whichone);

}

****结束接口代码


*****实施代码*****

在MyImplementationClass中我们有

enum ImyInterface.MyJamChoice

{

草莓,

Apricot

}

bool ImyInterface.JamInStock(ImyInterface.MyJamChoice whichone)

{

//果酱检查代码如果有货则返回true

}

**********实施代码结束*******


****客户代码***

ImyInterface = new MyImplementationClass()

//此时Intellisense知道枚举内容

if(ImyInterface.JamInStock(ImyInterface.MyJamChoice。 StrawBerry)

{//让我们吃早餐}

****客户代码结束****

问候

Bob

" Mattias Sj?gren" < ma ******************** @ mvps.org写信息

新闻:e6 ********** **** @ TK2MSFTNGP02.phx.gbl ...
Hi Mattias,
I see it happening in much the same way as any other interface
implementation.
i.e.

****Interface code *****
public Interface IMyInterface
{
enum MyJamChoice{};
bool JamInStock(MyJamChoice whichone);
}
****End Interface Code

*****Implementation code*****
In MyImplementationClass we have
enum ImyInterface.MyJamChoice
{
Strawberry,
Apricot
}
bool ImyInterface.JamInStock(ImyInterface.MyJamChoice whichone)
{
//Jam checking code return true if in stock
}
**********End of Implementation code *******

****Client Code***
ImyInterface = new MyImplementationClass()
//at this point Intellisense knows the enum contents
if (ImyInterface.JamInStock(ImyInterface.MyJamChoice. StrawBerry )
{//Lets have breakfast}
**** End of Client code****
regards
Bob
"Mattias Sj?gren" <ma********************@mvps.orgwrote in message
news:e6**************@TK2MSFTNGP02.phx.gbl...

>
>

忽略语法含义在我看来你应该能够
Ignoring the syntax implications it seems to me that you should be able



to

to


声明枚举存在。即它在
的''精神'内
declare that an enum exists. i.e. It is within the ''spirit'' of an



interface

interface


表明它存在。实施班将提供成员。
to state that it exists. The implementing class will provide the members.



如果你不知道它的
成员,你的客户代码会如何消耗enum?


Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


How would your client code consume the enum if you don''t know its
members?
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.



> ****客户代码***
>****Client Code***

> ImyInterface = new MyImplementationClass()
>ImyInterface = new MyImplementationClass()



我应该假设ImyInterface是IMyInterface类型的变量

还是什么?

Should I assume that ImyInterface is a variable of type IMyInterface
or what?


> //此时Intellisense知道枚举内容
if(ImyInterface.JamInStock(ImyInterface.MyJamChoice。StrawBerry)
>//at this point Intellisense knows the enum contents
if (ImyInterface.JamInStock(ImyInterface.MyJamChoice. StrawBerry )



它有吗?如果ImyInterface确实是IMyInterface类型的变量,

编译器对具体实现一无所知

(MyImplementationClass)你是使用接口这就是

的重点 - 能够编写相同的代码而不管

的实现。

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。

It does? If ImyInterface is indeed a variable of type IMyInterface,
the compiler knows nothing about the concrete implementation
(MyImplementationClass) you''re using. That''s kind of the point of
using interfaces - to be able to write the same code regardless of
implementation.
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


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

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