为什么切换不能用于对象 [英] Why can't switch be used for objects

查看:82
本文介绍了为什么切换不能用于对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能切换用于对象,至少对于静态只读字段它是好的。

Jit当然可以优化这种情况,因为地址静态

只读变量在jit时间是已知的。


事情就是经常使用我自己的枚举类,因为我通常想要

为枚举成员分配一个用户友好(和可本地化)的名称,如果我将枚举成员添加到组合框中,则会自动显示



另外我有帮助方法给我返回一个列表中的所有或一个

枚举成员的特定子集:


公共类颜色

{

public static reaonly Color Red = new Color(255,0,0);

public static reaonly Color Green = new Color(0,255,0);

//等等


private Color(){}


public Color [] GetColors()。 。

公共颜色[] GetGreenLikeColors()..

p ublic Color [] GetDarkColors()..

public Color [] GetSystemColors()..


Color GetByName(string name){}

颜色GetByID(字符串名称){}

}


getXX方法主要使用哈希表,最棒的是

如果使用泛型,我可以从一些基础枚举类派生所有枚举,而不需要添加新的getXX方法,这些方法返回Color []而不是object []。


愚蠢的事情是,如果我将一些enum更改为我的枚举类,我总是将

重写为巨大的丑陋,如果'。'


我看到没有技术上的原因为什么不允许切换对象。

当然不会像使用常数int值一样快但是无论如何他们

允许在switch语句中使用字符串。


在像C#这样的现代语言中,switch语句的目的不应该是
来使程序更快但是更具可读性。


我只是简单的愚蠢还是有一些我忽略的东西?

解决方案

我对你的帖子感到困惑,因为我没有看到任何枚举

在其中任何地方声明。但是,如果我一般了解你的b / b $ b问题的性质,那么我有一个问题:


为什么有一些外部代理(类外的一些方法) ),通过你的对象筛选

,试图决定返回哪个枚举?为什么

并不是班级本身为你提供适当的枚举。对于

示例:


enum ColorEnum

{

未指定,

红色,

绿色,

蓝色

}


颜色r =新颜色( 255,0,0,ColorEnum.Red);

颜色g =新颜色(0,255,0,ColorEnum.Green);

颜色b =新颜色(0, 0,255,ColorEnum.Blue);


然后你只需说:


ColorEnum en = g.EquivalentEnum;

无需切换或ifs。作为一般规则,类应设计为
,以便对象了解自己。如果你发现自己在客户代码中编写任何类型的复杂测试,那么你的课程就不够强大了,这是一个很大的问题。这也是对未来

维护问题的一个暗示,因为您的客户端代码包含知道太多的业务逻辑

。关于你的对象;此类代码属于

类本身。




" Bruce Wood" <峰; br ******* @ canada.com> schrieb im Newsbeitrag

新闻:11 ********************** @ g43g2000cwa.googlegr oups.com ...

我对你的帖子感到困惑,因为我没有看到任何枚举在其中的任何地方。但是,如果我一般了解你的
问题的性质,那么我有一个问题:


Color类是我的Enum。这不是一个像ac#enum这样的枚举但是它有效吗

就像枚举一样,虽然存在差异。

为什么有一些外部代理(课外的一些方法),筛选
通过你的对象试图决定返回哪个枚举?为什么
并不是课程本身为你提供适当的枚举。对于
示例:

枚举ColorEnum
{
未指定,
红色,
绿色,
蓝色
}

颜色r =新颜色(255,0,0,ColorEnum.Red);
颜色g =新颜色(0,255,0,ColorEnum.Green);
颜色b =新颜色(0,0,255,ColorEnum.Blue);

然后你只需说:

ColorEnum en = g.EquivalentEnum;

否切换或ifs需要。作为一般规则,应该设计类,以便对象了解自己。如果你发现自己在客户代码中编写任何类型的复杂测试,那么这是一个很大的暗示你的课程不够强大。它也暗示了未来的维护问题,因为您的客户端代码包含知道太多的业务逻辑。关于你的对象;这样的代码属于
类本身。




您如何将ColorEnum转换为Color对象?


> Color类是我的枚举。这不是一个像ac#enum这样的枚举,但它的作用就像一个枚举,虽然存在差异。


好​​的......然后我需要一些代码来说明那些大丑 ifs''"

因为我不明白你原来的问题。

你觉得如何将ColorEnum转换为Color对象?




好​​吧,如果你这样做,那么Color中的静态方法就可以做到这一点:


公共静态Color ColorForEnum(ColorEnum en)

{

}


里面你可以使用通过询问

每个颜色它所属的枚举来切换或设置集合。


无论如何,你不是那样做的。所以,给我发一些代码,这样我的大脑可以解决你的问题。 :-)


Why can''t switch used for objects, at least for static readonly fields it
wold be nice.
The Jit could certainly optimize this case because the addresses of static
readonly variables are known at jit time.

The thing is that is often used my own enum classes because I usually want
to assign a userfriendly (and localizable) name to an enum member which is
automatically displayed if I add a enum member to a combobox for example.
Additionally I have helper methods for returning me a list of all or a
specific subsets of the member of the enums:

public class Color
{
public static reaonly Color Red = new Color(255,0,0);
public static reaonly Color Green = new Color(0,255,0);
// and so on

private Color(){}

public Color[] GetColors() ..
public Color[] GetGreenLikeColors() ..
public Color[] GetDarkColors() ..
public Color[] GetSystemColors() ..

Color GetByName(string name){}
Color GetByID(string name){}
}

The getXX methods work mainly using hashtables and the great thing is that
if using generics I could derive all enums from some base enum class without
having to add new getXX methods which return Color[] instead of object[].

The stupid thing is if I change some enum into my enum class I always have
to rewrite all switches into huge ugly if''s.

I see no technical reason why switch shouldn'' be allowed for objects.
Certainly is won''t be as fast as with constant int values but anyway they
allowed strings in switch statements.

In a modern language like C# the purpose of a switch statement shouldn''t be
to make a program faster but more readable.

Am I just plain stupid or is there something huge I overlooked?

解决方案

I''m a little confused by your post, since I don''t see any enums
declared anywhere in it. However, if I understand the nature of your
problem in general, then I have a question:

Why is some outside agent (some method outside the class), sifting
through your objects in an attempt to decide which enum to return? Why
doesn''t the class itself supply you with its appropriate enum. For
example:

enum ColorEnum
{
Unspecified,
Red,
Green,
Blue
}

Color r = new Color(255,0,0,ColorEnum.Red);
Color g = new Color(0,255,0,ColorEnum.Green);
Color b = new Color(0,0,255,ColorEnum.Blue);

Then you just say:

ColorEnum en = g.EquivalentEnum;

No switch or ifs needed. As a general rule, classes should be designed
so that the object knows a lot about itself. If you find yourself
writing complicated tests of any kind in client code then it''s a big
hint that your class isn''t beefy enough. It''s also a hint at a future
maintenance headache, because your client code contains business logic
that "knows too much" about your objects; such code belongs within the
class itself.



"Bruce Wood" <br*******@canada.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...

I''m a little confused by your post, since I don''t see any enums
declared anywhere in it. However, if I understand the nature of your
problem in general, then I have a question:
The Color class IS my Enum. Is it not a enum like a c# enum but it works
like an enum though there are differences.
Why is some outside agent (some method outside the class), sifting
through your objects in an attempt to decide which enum to return? Why
doesn''t the class itself supply you with its appropriate enum. For
example:

enum ColorEnum
{
Unspecified,
Red,
Green,
Blue
}

Color r = new Color(255,0,0,ColorEnum.Red);
Color g = new Color(0,255,0,ColorEnum.Green);
Color b = new Color(0,0,255,ColorEnum.Blue);

Then you just say:

ColorEnum en = g.EquivalentEnum;

No switch or ifs needed. As a general rule, classes should be designed
so that the object knows a lot about itself. If you find yourself
writing complicated tests of any kind in client code then it''s a big
hint that your class isn''t beefy enough. It''s also a hint at a future
maintenance headache, because your client code contains business logic
that "knows too much" about your objects; such code belongs within the
class itself.



And how do you think to convert a ColorEnum to a Color object?


> The Color class IS my Enum. Is it not a enum like a c# enum but it works like an enum though there are differences.

OK... then I need some code to illustrate those "big ugly ''ifs''"
because I don''t understand your original problem.

And how do you think to convert a ColorEnum to a Color object?



Well, if you were doing things that way, a static method in Color would
do the trick:

public static Color ColorForEnum(ColorEnum en)
{
}

inside there you could use a switch or set up collections by asking
each Color what enum it belonged to.

Anyway, you''re not doing things that way. So, post me some code so my
poor brain can get a handle on your problem. :-)


这篇关于为什么切换不能用于对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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