问题描述
我正在尝试一个很好的例子来详细说明Ada的严格打字和Java的强类型之间的区别。
有没有人有一个很好的例子来确定使用基于整数的值在类型转换中的这些差异?
I'm trying to think of a good example detailing the difference between Ada's strict typing and Java's strong typing.Does anyone have a good example for identifying these differences in type conversion using integer based values?
推荐答案
这可能证明从Ada的角度来看很有帮助。
This may prove helpful, from the Ada perspective.
类型声明有两种基本形式:新类型和子类型。
There are two basic forms of a type declaration : the new type, and the subtype.
新类型被认为是与以前任何类型完全不同的类型,即使它具有相同的值范围。新类型的对象只能分配给新类型的变量,依此类推。
A new type is regarded as a completely different type from any previous type, even if it has the same range of values. Objects of the new type can only be assigned to variables of the new type, and so on.
子类型派生自现有类型,共享其值的子集,就作业而言,它被认为是基本上相同的类型。
A subtype is derived from an existing type, shares a subset of its values, and is regarded as being essentially the same type as far as assignment is concerned.
使用类型系统的艺术需要一些练习,而且 - 在类型之间进行选择和亚型 - 是重要的一个领域。它使得类型系统受到挫败,并且让程序简单轻松地组合在一起。
The art of using the type system takes a little practice, and this – choosing between types and subtypes - is one area where it matters. It makes the difference between frustratedly fighting the type system, and having programs fit together simply and easily.
- 声明一个新类型,其值表示新的东西,你不需要或想要与其他数量混合的东西
。 - 声明一个子类型,其值与现有事物有关。
一个例子:想象一下自动化建筑物,包括其升降系统:
An example : imagine automating a building, including its lift system :
type Floor is new Integer range -3 ..135;
有几个地下车库,一个地下室,0楼是底层(对于欧洲人来说)比美国建筑!和它上面的135层楼高,比高耸的地狱高一层。
There are a couple of underground garages, a basement, 0 is the ground floor (for an European rather than American building!) and 135 floors above it, making it one floor taller than the Towering Inferno.
这可以安全地成为一种新型,因为几乎没有混淆的危险一个Floor变量与其他任何东西,并且很少需要在数学上将Floor与任何其他数量相结合。升降机需要去那里,而这就是它。
在这种情况下,将Floor作为新类型可以捕获一些错误,但增加的安全性是值得的,没有痛苦的前景。
This can safely be a new type because there is little danger of confusing a Floor variable with anything else, and very little need to mathematically combine a Floor with any other quantity. Lifts need to go there, and that's about it.In this case, having Floor as a new type can catch some errors, but the added safety is worthwhile without the prospect of pain.
使用类型或子类型作为数组索引和循环边界是一种不太可能进行越界访问的好方法。
Using types or subtypes as array indexes and loop boundaries is a good way to make out-of-bounds accesses rather unlikely.
Array(Floor) of ...
for f in Floor loop ...
如果你确实需要从一个整数变量赋值给一个Floor,那么类型转换会向编译器和任何读取代码的人发出信号。
If you do need to assign from an integer variable to a Floor, a type conversion signals to both the compiler and anyone reading the code that this as intentional.
subtype Population is Natural range 0 .. 10000;
我们必须知道有多少人占用建筑物,以确保安全,防火,供暖或制冷需求,和其他目的。它的范围在两端都有严格的限制:对于这个建筑,我们假设有超过10000名乘客被消防安全法规禁止。
We must know how many people occupy the building, for security, fire safety, heating or cooling requirements, and other purposes. Its range has hard limitations at both ends : for this building, let's assume more than 10000 occupants have been prohibited by fire safety regulations.
基本类型是自然的(本身是Integer的子类型而不是Integer,因为总体不能小于零。
The base type is Natural (itself a subtype of Integer) rather than Integer, because the population can never be less than zero.
但是将Population设为新类型可能会导致无穷无尽的类型转换问题。将其作为一个子类型,可以更容易地在热负荷计算中使用群体,提升调度策略,用水预测,以及一些例子。
But making Population a new type might cause endless type conversion issues. Making it a subtype makes it easier to use Population in heat load calculations, lift scheduling strategies, water use predictions, for a few examples.
它是一个子类型的事实而不只是Integer仍然提供有用的保护形式;虽然整数可以分配给Population变量并在表达式中与它们混合,但任何分配超出范围值的尝试都将被标记为错误。如果在编译时无法检测到它们,则它们将在运行时引发异常。第10001名试图进入大楼的人也是如此。
The fact of its being a subtype rather than just Integer still provides useful forms of protection; while integers can be assigned to Population variables and mixed with them in expressions, any attempt to assign out-of-range values will be flagged as errors. If they cannot be detected at compile time, they will raise an exception at runtime. And so will the 10001th person attempting to enter the building.
我将让Java专家解释用Java实现这些策略的最佳方法。
I'll let the Java experts explain the best way to achieve these strategies in Java.
这篇关于示例代码详细说明了Ada和Java之间的输入差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!