Hướng dẫn cách dùng

Object-Oriented Programming
NetTradeX PC
NetTradeX Android
NetTradeX iOS
NetTradeX Mobile
NetTradeX Advisors
Object-Oriented Programming

Classes allow the programmer to define and use new data types that will contain the properties and methods implementing certain algorithms. For this purpose, the NetTradex language supports classes and concepts such as encapsulation, inheritance, and polymorphism. Next, let us demonstrate the process of creating custom classes and working with them.

To create a class it needs to be declared. Declaration includes:

  • reserved keyword class
  • class name
  • class body, enclosed in curly braces

Class methods can access its properties, either directly (by the identifier) or using the reserved word this , which is the reference to curren object instance.

While creating a class instance, the special class method (the constructor) is called. The constructor name must match the name of the class, besides the constructors have no return value. A class can have several constructors and they must differ by type and/or number of parameters. You cannot call one constructor from another if they are constructors of the same class.

The class methods can be accessed via the operator.(point), for example, to gain access to the max_price property for the current_day class instance, use current_day.max_price. Typically, access to the properties of object is not gained directly and special methods should be created: getters and setters.

After declaring the class, create an instance of the class object. This can be done by specifying Class_Name Object_Name;

Let's create a class that will display information about Bid and Ask prices:

class CurrencyPair
{
    // class properties
	string symbol;
	double bid;
	double ask;
	double point;
	
	// class method 
	// Displaying the class properties value 
	void Print()
	{
		System.Print("Symbol="+symbol+" Bid="+bid+" Ask="+ask+" Point="+point);
	}
	// constructor 
	CurrencyPair(string symbol)
	{
		this.symbol = symbol;
		ask = Symbols.LastAsk(symbol);
		bid = Symbols.LastBid(symbol);
		point = Symbols.Point(symbol);
	}	
}
int Run()
{
  	CurrencyPair x(Chart.Symbol);
 	x.Print();
  	return(0);
}

Functions and methods of the class can be specified as private . In this case, they become invisible to objects outside the class and cannot be used directly by the operator .(point).

class AccountState
{
    private double balance;
    private double equity;
    private datetime t;
    AccountState()
    {
    	balance = Account.Balance;
    	equity = Account.Equity;
    	t = System.Time;
    }
    void Print()
    {
    	System.Print("Balance="+balance+" Equity="+equity+" Time="+t);
    }
}
int Run()
{
    AccountState a;
    //a.equity = 100000; Prohibited because of the private access modifier 
    a.Print();
    return(0);
}