Ada 的 T'Class 基础知识 [英] Fundamentals of Ada's T'Class

查看:25
本文介绍了Ada 的 T'Class 基础知识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问这个有点不好意思,但我知道这是最好的.我已经在 Ada 中编程多年,并且几乎可以流利地理解该语言的每个部分.然而,我似乎从来没有能够把我的头放在 T'Class 上.借用别人,有人能像我五岁那样解释吗?".

Somewhat embarassed to ask this, but I know it's for the best. I've been programming in Ada for many years now, and understand nearly every part of the language fluently. However, I've never seemed able to wrap my head around T'Class. To borrow from others, can someone "explain it like I'm five?".

我买它只是为了拥有,但其中包含对 T'Class 的精彩描述和示例使用;我参考了 Michael B. Feldman 的Ada 95 的软件构建和数据结构".

I bought it just to have, but contained within is a great description of, and example use of, T'Class; I refer to "Software Construction and Data Structures with Ada 95" by Michael B. Feldman.

推荐答案

如果开始

package P1 is
   type T is tagged private;
   procedure Method (Self : T);
end P1;
package P2 is
   procedure Proc (Self : T);  -- not a primitive
   procedure Proc2 (Self : T'Class);
end P2;

在 Proc 的情况下,您告诉编译器应该始终将参数视为 T 类型(请记住,标记类型始终通过引用传递,因此实际类型当然可以从 T 派生,您不会丢失额外的数据).特别是,这意味着在 Proc 的主体内,对 Method 的所有调用都将完全是对 P1.Method 的调用,而不是对覆盖 Method 的调用.

In the case of Proc, you are telling the compiler that the parameter should always be considered precisely as of type T (remember that a tagged type is always passed by reference, so the actual type could be derived from T of course, you would not lose the extra data). In particular, that means that within the body of Proc, all calls to Method will be exactly calls to P1.Method, never a call to an overriding Method.

在 Proc2 的情况下,你告诉编译器你不知道静态的确切类型,所以它需要插入额外的代码来在运行时解决问题.在 Proc2 的主体内对 Method 的调用可以是对 P1.Method 或另一个覆盖方法的调用.

In the case of Proc2, you are telling the compiler that you do not know the exact type statically, so it will need to insert extra code to resolve things at run time. A call to Method, within the body of Proc2, could be call to P1.Method, or to another overriding Method.

基本上:使用 'Class,事情在运行时得到解决.

Basically: with 'Class, things are resolved at runtime.

这篇关于Ada 的 T'Class 基础知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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