MT5入门到精通之六(MQL5高级语法 )

2017/4/22 19:32
MT5入门到精通之六(MQL5高级语法 )
一.类
面向对象
1.封装(不让外部使用,private)
private类内部使用,public内部外部都可以访问,protect继承类也可以访问,一定要在类内访问。
2.函数重载,同一个函数名,不同参数

3.继承
执行顺序 父类的构造 -子类的构造-子类的析构-父类的析构[(注意法1创建对象方法不是立马自动调用析构函数的,没有使用对象后才自动调用的)]

MT5入门到精通之六(MQL5高级语法 )_第1张图片
Paste_Image.png

4.重写,子类重写父类的函数

5.多态
一般我们把父类函数申明程虚函数virtual void work()

  //4.多态 (用父类声明,用子类创建)
  Person *mp = new PersonMale();
  //4.1如果父类函数不做特殊处理,这里会调用父类的work函数【按这里的创建应该调用的是子类的work】
  mp.work();
  delete mp;

6.纯虚函数
如果没有函数体的虚函数就是纯虚函数,子类一定要实现它(抽象类不能实例化 )

virtual void say()=NULL;

7.有纯虚函数的类就是抽象类,抽象类不能实例化


MT5入门到精通之六(MQL5高级语法 )_第2张图片
Paste_Image.png

二.具体测试代码
1.父类Person

//+------------------------------------------------------------------+
//|                                                       Person.mqh |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Person
  {
private:
   int               bI;
   double            cD;
   //5.类静态成员变量
   static int        height;

   int add(int x,int y)
     {
      return(x+y);
     }
   //2.添加const 说明该值在函数里面不能改变
   int add(const int x,int y,int z)
     {
      return(x+y+z);
     }
   //2.1添加const函数申明,不会对成员变量做任何改变
   void testConst() const
     {
     }
protected:
   int               p;
   void testProtect()
     {
     }

public:
   //3.函数重载(函数名一样,参数不一样)
                     Person(int b,double c)
     {
      this.bI=b;
      this.cD=c;
      printf(__FUNCTION__);
     }
   //6.类构造函数
                     Person()
     {
      printf(__FUNCTION__);
     }
   //6.1类析构函数
                    ~Person()
     {
      printf(__FUNCTION__);
     }

   //4.静态成员函数(不需要创建对象,可以直接使用)
   static int multiply(int x,int y)
     {
      return x*y;
     }
   //1.1方法先申明
   void              print();

   //7.虚函数设置 不会调用父类这个函数
   virtual void work()
     {
      printf("person work");
     }

   //7.1如果没有函数体的虚函数就是纯虚函数,子类一定要实现它[没有null就不是纯虚函数]
   virtual void      say();
   // virtual void say()=NULL;
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//1.2 方法后实现
void Person::print(void)
  {
   printf("我是Person");
   printf(__FUNCTION__);
  }

//5.1类外面直接给静态成员变量赋值
int Person::height=176;
//+------------------------------------------------------------------+

2.子类

//+------------------------------------------------------------------+
//|                                                   PersonMale.mqh |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//1.继承父类
class PersonMale:public Person
  {
private:

public:
                     PersonMale();
                    ~PersonMale();

   //3.重写父类的函数
   void work()
     {
      printf("male work");
     }
   void say()
     {
      printf(__FUNCTION__);
     }
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//2.指明调用父类的那个构造函数
PersonMale::PersonMale():Person(2,3.5)
  {
//1.1调用父类的protect成员变量和函数
   p=12;
   testProtect();
   printf(__FUNCTION__);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
PersonMale::~PersonMale()
  {
   printf(__FUNCTION__);
  }
//+------------------------------------------------------------------+

class PersonFemale:public Person
  {
private:
protected:
public:
                     PersonFemale();
                    ~PersonFemale();
   void say()
     {
      printf(__FUNCTION__);
     }
  };
//+------------------------------------------------------------------+
PersonFemale::PersonFemale(void)
  {
   printf(__FUNCTION__);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
PersonFemale::~PersonFemale(void)
  {
   printf(__FUNCTION__);
  }
//+------------------------------------------------------------------+

3.测试脚本代码

//+------------------------------------------------------------------+
//|                                              testClassScript.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
//1.类的创建
//1.1(自动调用析构函数)推荐用
//Person aC=10;
   Person aC(10,1.2);
//1.2 需要自己释放内存
   Person *bC=new Person(2,1.2);
   delete bC;

//1.3初始化对象数组
   Person persons[10];
//2.类静态函数使用
   int resI=Person::multiply(5,6);

/////////////////////////
//3.继承
   PersonMale maleC();
//3.1直接使用父类public公开的函数或成员变量(其它都调用不了)
   maleC.print();
   maleC.work();


//4.多态 (用父类声明,用子类创建)
   Person *mp=new PersonMale();
//4.1如果父类函数不做特殊处理,这里会调用父类的work函数【按这里的创建应该调用的是子类的work】
   mp.work();
   delete mp;

  }
//+------------------------------------------------------------------+

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(MT5入门到精通之六(MQL5高级语法 ))