代码之家  ›  专栏  ›  技术社区  ›  Charlie-Greenman

OOP-如何在内部合理地调用类方法

  •  1
  • Charlie-Greenman  · 技术社区  · 8 年前

    我有一个 testFactory 班其目的是能够传入工厂,然后控制台输出结果以进行演示。到现在为止,当试图打电话时 createProductA 在测试方法中,编译器会抱怨createProductA未绑定( Unbound value createProductA ).

    在类中内部调用方法的正确语法是什么?

    class testFactory (factory: abstractFactory) => {
      as _;
      pub createProductA => factory#createProductA;
      pub createProductB => factory#createProductB;
    
      pub test () => {
        Js.log createProductA;
        Js.log createProductB;
      }
    };
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   glennsl Namudon'tdie    8 年前

    这就是 as _; 如果您曾经想知道这是为了什么,那么类定义的一部分就会出现。

    createProductA createProductB this self ,但让你去做,这正是 as 是的,并且 _ 作为_; 例如:。 as self; 你可以参考 自己

    试试这个:

    class testFactory (factory: abstractFactory) => {
      as self;
      pub createProductA => factory#createProductA;
      pub createProductB => factory#createProductB;
    
      pub test () => {
        Js.log self#createProductA;
        Js.log self#createProductB;
      }
    };