简体   繁体   English

OOP - 如何在Reason内部调用类方法

[英]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. createProductAcreateProductB是方法,而不是函数,因此需要在对象上调用它们。 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中不会自动绑定到当前的对象名称类似thisself ,而是把它放在你做到这一点,这恰恰是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM