[英]Self-Referencing in Ada that is similar to Java “this”?
In Java we can use "this" to self-reference an object. 在Java中,我们可以使用“ this”自引用对象。 Is there an equivalent in Ada?
阿达有等同的吗? Or do all parameters need to be passed explicitly?
还是所有参数都需要显式传递?
There isn't an equivalent in Ada, because the mechanism for defining operations of an object is different. Ada中没有等效项,因为定义对象操作的机制不同。 In Java and most other well-known OO languages, the method definitions are part of the type (class) definition:
在Java和大多数其他知名的OO语言中,方法定义是类型(类)定义的一部分:
public class MyClass {
public String getName() { ... }
}
getName
is defined inside MyClass
, and therefore is an instance method that operates on a MyClass
object. getName
在MyClass
内部定义,因此是对MyClass
对象进行操作的实例方法 。 In effect, the code for getName()
has an implicit parameter of type MyClass
, and within the body of getName()
, the keyword this
is used to refer to that implicit parameter. 实际上,对于代码
getName()
具有类型的隐式参数MyClass
,和体内getName()
关键字this
被用来指代隐式参数。 Some other languages use self
instead of this
. 其他一些语言使用
self
代替this
。
Ada doesn't do things this way; 艾达(Ada)并非以这种方式做事; you can't define a
procedure
or function
inside the record type definition (although protected type definitions allow this). 您不能在记录类型定义中定义
procedure
或function
(尽管受保护的类型定义允许这样做)。 To define an operation on an object, the procedure
or function
is defined outside the record
type (or type extension) definition, with one of the parameters, usually the first (*), being the record type: 要定义对对象的操作,
procedure
或function
是在record
类型(或类型扩展)定义之外定义的,其中一个参数(通常是第一个(*))是记录类型:
type My_Class is tagged record
...
end record;
function Get_Name (Obj : My_Class) return String;
-- note that this is outside the "record" .. "end record"
The explicit parameter is how Ada knows that this is an operation of My_Class
--there's no other way to tell it, since the function doesn't have to follow the record definition immediately. 显式参数是阿达怎么知道这是一个操作
My_Class
--there没有其他的方式来告诉它,因为该功能不必紧跟在记录定义。 Since there's no implicit parameter, you just use the explicit parameter's name, Obj
, to refer to the object. 由于没有隐式参数,因此只需使用显式参数的名称
Obj
来引用对象。 Many Ada programmers use the names This
or Self
as the parameter name to make it look similar to other languages. 许多Ada程序员使用名称
This
或Self
作为参数名称,以使其看起来与其他语言相似。
(*) A procedure or function can still be an operation of My_Class
, if any parameter has type My_Class
and it's defined in the same "declaration list" as the My_Class
type. (*)如果任何参数的类型为
My_Class
并且在与My_Class
类型相同的“声明列表”中定义,则过程或函数仍可以是My_Class
的操作。
type My_Class is tagged record .. end record;
function Get_Something (N : Integer; Obj : My_Class) return String;
This is a primitive operation of My_Class
, and it is polymorphic (it can dispatch to overriding functions of derived types), but you can't say X.Get_Something(3)
because the My_Class
parameter isn't the first parameter. 这是
My_Class
的原始操作,它是多态的(可以分派给派生类型的重写函数),但是您不能说X.Get_Something(3)
因为My_Class
参数不是第一个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.