简体   繁体   English

将构造关键字添加到方法时会做什么?

[英]What does the construct keyword do when added to a method?

The code here is X++. 这里的代码是X ++。 I know very little about it, though I am familiar with C#. 尽管我对C#熟悉,但我对此知之甚少。 MS says its similiar to C++ and C# in syntax. MS说它在语法上类似于C ++和C#。

Anyway, I assume the code below is a method. 无论如何,我认为下面的代码是一种方法。 It has "Construct" as a keyword. 它以“构造”作为关键字。

What is a construct/Constructor method? 什么是构造/构造方法? What does the construct keyword change when applied to the function? 构造关键字应用于函数时会发生什么变化? Also, am I wrong in assuming the code would create some sort of infinite loop? 另外,我是否认为代码会创建某种无限循环是错误的?

My assumption is that its a method with a return type of "InventMovement". 我的假设是它的返回类型为“ InventMovement”的方法。

static InventMovement construct(Common buffer, InventMovSubType subType = InventMovSubType::None, Common childBuffer = null)
{
    InventMovement movement = InventMovement::constructNoThrow(buffer,subType,childBuffer);

    if (!movement)
        throw error("@SYS20765");

    return movement;
}

Thanks! 谢谢! Kevin 凯文

Construct is not a keyword in X++, this is merely a static method called construct that returns an InventMovement class. 在X ++中,Construct不是关键字,这只是一个静态方法,称为construct ,它返回InventMovement类。 It is used to allow you to create a derived class of a base class without having to know which derived class to create. 它用于允许您创建基类的派生类,而不必知道要创建哪个派生类。 This is how AX implements the Factory pattern. 这就是AX实现Factory模式的方式。 You will see this pattern used in AX in many places where there are abstract base classes. 您将在许多有抽象基类的地方看到AX中使用的这种模式。

InventMovement is an abstract base class for many other classes, such as InventMov_Purch and InventMov_Sales . InventMovement是许多其他类的抽象基类,例如InventMov_PurchInventMov_Sales You can't call new() on an abstract class, so instead of having a switch statement to call either new InventMov_Purch() or new InventMov_Sales() every time you need to create a InventMovement class, you use the InventMovement::construct() method to call the correct new() for you. 您不能在抽象类上调用new(),因此,每次需要创建InventMovement类时,都不必使用switch语句来调用new InventMov_Purch()new InventMov_Sales() ,而是使用InventMovement::construct()方法为您调用正确的new()。

There is no construct keyword in X++. X ++中没有构造关键字。 The consturct idiom it's just how you implement the Factory design pattern in X++. 构造习语就是您在X ++中实现Factory 设计模式的方式 Usually you'll find the 'construct' method in the base (abstract) class of the inheritance tree, and its purpose is to simply construct (instanciate and sometimes initialize) the correct subclass. 通常,您会在继承树的基类(抽象)中找到“ construct”方法,其目的是简单地构造(实例化和初始化)正确的子类。 After the correct subclass is created, the construct method returns a base class reference to the already created subclass. 创建正确的子类后,construct方法将基类引用返回到已创建的子类。 This is called polymorphism . 这称为多态

The construct idiom in X++ (rougly) translates to the following C++ pseudo-code: X ++中的构造习惯用法(不好意思)转换为以下C ++伪代码:

class Base
{
public:
    static Base * construct(int aType);
    //some instance methods here
}; 

class Concrete1 : public Base
{
    // concrete implementation 1
};

class Concrete2 : public Base
{
    // concrete implementation 2
};

Base * Base::construct(int aType)
{
    switch(aType)
    {
        case 1:
            return  (Base*) new Concrete1();
        case 2:
            return (Base*) new Concreate2();
    }
}

DISCLAIMER: I know nothing about X++. 免责声明:我对X ++一无所知。

Based on the example there, it seems that the construct keyword is creating a constructor method. 根据那里的示例,似乎construct关键字正在创建构造方法。

In C++, you'd have 在C ++中,您将拥有

static Foo::Foo(int arg1) { this->val = arg1 }

My guess is that, for whatever reason, X++ requires the construct keyword on its constructor methods. 我的猜测是,无论出于何种原因,X ++都需要在其构造方法上使用Construct关键字。 It also appears that there's a constructNoThrow keyword, which presumably would be a constructor that's guaranteed not to throw an exception. 似乎还存在一个ConstructNoThrow关键字,该关键字大概是保证不会引发异常的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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