Ada中的派生类型和子类型 [英] Derived types and sub types in Ada

查看:85
本文介绍了Ada中的派生类型和子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别?

推荐答案

首先,术语:是 Ada,而不是 ADA-以 Ada Lovelace的名字命名;

First of all, terminology: it's "Ada", not "ADA" -- it's named after "Ada Lovelace"; it is not an acronym.

子类型与其基本类型兼容,因此可以将基本类型的操作数与基本类型的操作数混合。例如:

A subtype is compatible with its base type, so you can mix operands of the base type with operands of the base type. For example:

subtype Week_Days is Integer range 1..7;

由于这是子类型,因此您可以(例如)添加 1 到一个工作日即可获得下一个工作日。

Since this is a subtype, you can (for example) add 1 to a weekday to get the next weekday.

派生类型是一种完全独立的类型,其特征与基本类型相同。您不能将派生类型的操作数与基本类型的操作数混合。例如,如果您使用:

A derived type is a completely separate type that has the same characteristics as its base type. You cannot mix operands of a derived type with operands of the base type. If, for example, you used:

type Week_Day is new Integer range 1..7;

然后,您可以在工作日中添加整数得到另一个工作日。要对派生类型进行操作,通常需要自己定义这些操作(例如,创建程序包)。同时,派生类型会继承其基本类型的所有操作(即使有些操作可能没有意义),因此您仍然会获得加法。

Then you would not be able to add an integer to a weekday to get another weekday. To do manipulations on a derived type, you'd normally define those manipulations yourself (e.g., create a package). At the same time, a derived type does "inherit" all the operations of its base type (even some that may not make sense) so you do still get addition.

这篇关于Ada中的派生类型和子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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