[英]OOP - How to call a class method internally in Reason
I have a testFactory
class. 我有一个testFactory
类。 The purpose is to be able to pass in a factory, and then console out results for demoing purposes. 目的是能够传入工厂,然后控制输出结果以进行演示。 As of now, when trying to call createProductA
within the test method , the compiler will complain that createProductA is unbound ( Unbound value createProductA
). 截至目前,当试图在测试方法中调用createProductA
时,编译器会抱怨createProductA未绑定( Unbound value createProductA
)。
What is the proper syntax for calling a method internally in a class? 在类中内部调用方法的正确语法是什么?
class testFactory (factory: abstractFactory) => {
as _;
pub createProductA => factory#createProductA;
pub createProductB => factory#createProductB;
pub test () => {
Js.log createProductA;
Js.log createProductB;
}
};
This is where the as _;
这就是as _;
part of the class definition comes in, if you've ever wondered what that was for. 如果你想知道那是什么,那么课程定义的一部分就会出现。
createProductA
and createProductB
are methods, not functions, so they need to be called on an object. createProductA
和createProductB
是方法,而不是函数,因此需要在对象上调用它们。 Reason/OCaml won't automatically bind the current object to a name like this
or self
, but puts it on you to do it, which is precisely what as
does, and _
means, as usual, "I don't care about this". 原因/ OCaml中不会自动绑定到当前的对象名称类似this
或self
,而是把它放在你做到这一点,这恰恰是as
不和_
手段,像往常一样,“我不关心这个”。 So if you change as _;
所以,如果你改为as _;
to eg as self;
以as self;
you'll be able to reference self
as the current object elsewhere. 你将能够将self
引用为其他地方的当前对象。
Try this: 尝试这个:
class testFactory (factory: abstractFactory) => {
as self;
pub createProductA => factory#createProductA;
pub createProductB => factory#createProductB;
pub test () => {
Js.log self#createProductA;
Js.log self#createProductB;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.