为什么没有非整枚举? [英] Why no non-integral enums?

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

问题描述

为什么在非整枚举不能创建?我想知道,如果这
是语言设计决定,或者是否有与在编译器执行这一问题。

Why is it that non-integral enums cannot be created? I want to know if this is a language design decision, or if there are issues with implementing this in the compiler.

在换句话说,它是可行的实现非整枚举到语言,但只是没有一个合理的需要?或者,如果它是不可行的,但是是合理的,是什么阻碍的方式?

In other words, is it feasible to implement non-integral enums into the language, but there just isn't a justifiable need? Or if it isn't feasible but is justifiable, what impediment are in the way?

有人给我什么理由或理由是没有这样的可用骨感在C#中,漂亮的请。

Someone give me the skinny on what the reason or rationale is for not having this available in C#, pretty please.

推荐答案

语言只有设计者可以告诉你他们为什么不容许非整枚举,但我可以告诉你最可能的解释。

Only the designers of the language can tell you why they didn't allow for non-integral enumerations but I can tell you the most likely explanation.

原来的C没有做到这一点,因为这是不必要其目的(一个系统编程语言)。因为它是基于C,你可以使用类反正效仿C ++没有做到这一点。 C#可能不以同样的理由这样做。

The original C didn't do it because it was unnecessary for its purposes (a systems programming language). C++ didn't do it since it was based on C and you could emulate it with classes anyway. C# probably doesn't do it for the same reasons.

这是对我而言只是猜测,因为我并没有参与任何这些语言的设计。

This is just guesswork on my part since I wasn't involved in the design of any of those languages.

一旦你决定去的类(日期时间)了,还有什么理由不走一路。如果你想使用枚举的,你可以创建枚举为连续整数从零开始,并有使用这些枚举为指标DateTime值的数组。

Once you've decided to go the way of classes (DateTime), there's little reason to not go all the way. If you want to use enum's, you can create the enums as sequential integers starting with zero and have an array of DateTime values using those enums as indexes.

但我只是有其类中提供的整个套件和kaboodle度假类:常量,则get()函数基于这些常数等返回日期时间

But I'd just have a Holiday class which provided the whole kit and kaboodle within the class: the constants, the get() routine to return a DateTime based on those constants and so on.

这让你真正的封装和恰克完全落实,而不会影响接口的能力。为什么要扔掉的OOP的好处之一?

That gives you true encapsulation and the ability to chage the implementation totally without affecting the interface. Why toss away one of the benefits of OOP?

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

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