构造关键字在添加到方法时会执行什么操作?

时间:2023-01-04 00:44:04

The code here is X++. I know very little about it, though I am familiar with C#. MS says its similiar to C++ and C# in syntax.

这里的代码是X ++。虽然我熟悉C#,但我对此知之甚少。 MS说它在语法上类似于C ++和C#。

Anyway, I assume the code below is a method. It has "Construct" as a keyword.

无论如何,我假设下面的代码是一种方法。它将“Construct”作为关键字。

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?

什么是构造/构造方法?当应用于函数时,construct关键字会发生什么变化?此外,我错误地认为代码会创建某种无限循环?

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

3 个解决方案

#1


Construct is not a keyword in X++, this is merely a static method called construct that returns an InventMovement class. 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. You will see this pattern used in AX in many places where there are abstract base classes.

Construct不是X ++中的关键字,这只是一个名为construct的静态方法,它返回一个InventMovement类。它用于允许您创建基类的派生类,而无需知道要创建哪个派生类。这就是AX实现Factory模式的方式。您将在许多有抽象基类的地方看到AX中使用的这种模式。

InventMovement is an abstract base class for many other classes, such as InventMov_Purch and InventMov_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.

InventMovement是许多其他类的抽象基类,例如InventMov_Purch和InventMov_Sales。您不能在抽象类上调用new(),因此每次需要创建InventMovement类时,不要使用switch语句调用新的InventMov_Purch()或新的InventMov_Sales(),而是使用InventMovement ::构造( )为您调用正确的new()的方法。

#2


There is no construct keyword in X++. The consturct idiom it's just how you implement the Factory design pattern in X++. 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. After the correct subclass is created, the construct method returns a base class reference to the already created subclass. This is called polymorphism.

X ++中没有构造关键字。这个构造成语就是你如何在X ++中实现Factory设计模式。通常你会在继承树的base(abstract)类中找到'construct'方法,它的目的是简单地构造(instanciate,有时初始化)正确的子类。在创建正确的子类之后,构造方法返回对已创建的子类的基类引用。这称为多态性。

The construct idiom in X++ (rougly) translates to the following C++ pseudo-code:

X ++(rougly)中的构造习惯用法转换为以下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();
    }
}

#3


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. It also appears that there's a constructNoThrow keyword, which presumably would be a constructor that's guaranteed not to throw an exception.

我的猜测是,无论出于何种原因,X ++都需要构造函数方法的construct关键字。它似乎还有一个constructNoThrow关键字,可能是一个保证不会抛出异常的构造函数。

#1


Construct is not a keyword in X++, this is merely a static method called construct that returns an InventMovement class. 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. You will see this pattern used in AX in many places where there are abstract base classes.

Construct不是X ++中的关键字,这只是一个名为construct的静态方法,它返回一个InventMovement类。它用于允许您创建基类的派生类,而无需知道要创建哪个派生类。这就是AX实现Factory模式的方式。您将在许多有抽象基类的地方看到AX中使用的这种模式。

InventMovement is an abstract base class for many other classes, such as InventMov_Purch and InventMov_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.

InventMovement是许多其他类的抽象基类,例如InventMov_Purch和InventMov_Sales。您不能在抽象类上调用new(),因此每次需要创建InventMovement类时,不要使用switch语句调用新的InventMov_Purch()或新的InventMov_Sales(),而是使用InventMovement ::构造( )为您调用正确的new()的方法。

#2


There is no construct keyword in X++. The consturct idiom it's just how you implement the Factory design pattern in X++. 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. After the correct subclass is created, the construct method returns a base class reference to the already created subclass. This is called polymorphism.

X ++中没有构造关键字。这个构造成语就是你如何在X ++中实现Factory设计模式。通常你会在继承树的base(abstract)类中找到'construct'方法,它的目的是简单地构造(instanciate,有时初始化)正确的子类。在创建正确的子类之后,构造方法返回对已创建的子类的基类引用。这称为多态性。

The construct idiom in X++ (rougly) translates to the following C++ pseudo-code:

X ++(rougly)中的构造习惯用法转换为以下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();
    }
}

#3


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. It also appears that there's a constructNoThrow keyword, which presumably would be a constructor that's guaranteed not to throw an exception.

我的猜测是,无论出于何种原因,X ++都需要构造函数方法的construct关键字。它似乎还有一个constructNoThrow关键字,可能是一个保证不会抛出异常的构造函数。