枚举可以在 MQL4/MQL5 中有自己的方法吗?我可以将字符串作为值存储在枚举中吗?
Can an Enum have its own method in MQL4/MQL5. Can i store string in an Enum as a value?
我正在尝试编写一个代码,我想将 ENUM 用作 class 并在其上调用一些方法。例如 toString()。或者我可以用 Mql4/Mql5 语言将字符串值存储在枚举中吗?
否,枚举不能有方法。但是,您可以使用多态行为来模仿这个所需的属性。
#property strict
#property script_show_inputs
#include <Object.mqh>
//+------------------------------------------------------------------+
enum ENUM_STRAT {STRAT1, STRAT2};
//+------------------------------------------------------------------+
input ENUM_STRAT inpStrategy=STRAT1;
//+------------------------------------------------------------------+
class StrategyBase : public CObject
{
public: virtual string toString()=0;
};
//+------------------------------------------------------------------+
class Strategy1 : public StrategyBase
{
public: virtual string toString() override { return "Strategy #1"; }
};
//+------------------------------------------------------------------+
class Strategy2 : public StrategyBase
{
public: virtual string toString() override { return "Strategy #2"; }
};
//+------------------------------------------------------------------+
StrategyBase* initStrategy()
{
switch(inpStrategy)
{
case STRAT1:
return new Strategy1();
default:
return new Strategy2();
}
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
StrategyBase *strategy = initStrategy();
printf("The result of the toString() method is %s", strategy.toString());
delete strategy;
}
我正在尝试编写一个代码,我想将 ENUM 用作 class 并在其上调用一些方法。例如 toString()。或者我可以用 Mql4/Mql5 语言将字符串值存储在枚举中吗?
否,枚举不能有方法。但是,您可以使用多态行为来模仿这个所需的属性。
#property strict
#property script_show_inputs
#include <Object.mqh>
//+------------------------------------------------------------------+
enum ENUM_STRAT {STRAT1, STRAT2};
//+------------------------------------------------------------------+
input ENUM_STRAT inpStrategy=STRAT1;
//+------------------------------------------------------------------+
class StrategyBase : public CObject
{
public: virtual string toString()=0;
};
//+------------------------------------------------------------------+
class Strategy1 : public StrategyBase
{
public: virtual string toString() override { return "Strategy #1"; }
};
//+------------------------------------------------------------------+
class Strategy2 : public StrategyBase
{
public: virtual string toString() override { return "Strategy #2"; }
};
//+------------------------------------------------------------------+
StrategyBase* initStrategy()
{
switch(inpStrategy)
{
case STRAT1:
return new Strategy1();
default:
return new Strategy2();
}
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
StrategyBase *strategy = initStrategy();
printf("The result of the toString() method is %s", strategy.toString());
delete strategy;
}