|
|
|
|
|
- Overview
- Syntax of class constructor
- Note regarding the constructors
- Syntax of constructors for the base classes and members
- Notes
- Syntax of the Destructor method
Constructor and destructor
When creating a class in the code editor, the Constructor and Destructor methods are automatically created by default. - The Constructor method, if defined by the developer, is automatically called when instantiating an object. It is used to perform the initialization steps for the object or related to the object (assigning members, checks, ...)
- The Destructor method, if it is defined by the developer, is automatically called when deleting the object (exit from the procedure in which the object was instantiated). It can be used to free resources for example...
Remarks:- The Constructor methods can be public, protected or private.
- You have the ability to create some multi-syntax constructors. For more details, see Prototype overload.
Syntax of class constructor Declaration of constructorPROCEDURE [<Access>] Constructor([<Parameters>]) Details of syntax <Access>: Optional
3 access levels are available: - PUBLIC (by default): The constructor is accessible outside the class.
- PROTECTED: The constructor is accessible in the class and in the derived classes.
- PRIVATE: The constructor is accessible in the class only.
For more details, see Notes about the Constructors.Remark: To modify the scope of the Constructor, you have the ability to use the popup menu on the method in the project explorer. Declaration of the object: the constructor is automatically called<ObjectName> is [object] <Class Name> ([<Parameters>]) Details of syntax <ObjectName>
Name that identifies the instance of the class. <Class Name>
Name that identifies the class, defined when creating the class in the code editor. <Parameters>
Optional parameters of constructor. Dynamic instantiation: the constructor is automatically called<ObjectName> is dynamic [object] <ClassName> <ObjectName> = new <ClassName> ([<Parameters>]) Details of syntax <ObjectName>
Name that identifies the instance of the class. <Class Name>
Name that identifies the class, defined when creating the class in the code editor. <Parameters>
Optional parameters of constructor. Note regarding the constructors Scope and access of the Constructors The Constructor of a class can be public, protected or private.Inheritance: - A class with public Constructor can be inherited.
- A class with protected Constructor can be inherited.
- A class with private Constructor can be inherited.
Accessibility of private or protected Constructors: - An object of the class cannot be instantiated in a project code.
- An array whose elements are instances of the class cannot be declared in a project code.
- A member of the type of the class in a composite variable, a structure or another class cannot be declared in a class code or in a project code.
Important note: Regardless the scope of the Constructor, dynamic objects can always be declared in a project code. If the instantiation (the allocation) of the objects cannot be performed, the declaration of dynamic objects is always possible. Therefore, the following codes are always valid: // In these two case, the instantiation is not performed during the declaration. Object1 is dynamic Class1 Object2 is Class1 <-...
To instantiate objects in the project code, you have the ability to use a global method of the class. Example with a private Constructor: // Global method of ClassWithPrivateConstructor GLOBAL procedure GlobalMethodAllocation(...)  o is ClassWithPrivateConstructor RETURN o // *****************************************  // Retrieve an object of the class from a project code:  // Solution 1: o2 is dynamic ClassWithPrivateConstructor o2 = ClassWithPrivateConstructor.GlobalMethodAllocation(...)  // Solution 2: o3 is ClassWithPrivateConstructor <- CClassWithPrivateConstructor.GlobalMethodAllocation(...) Caution: The assignment operator (=) behaves differently with the dynamic objects and with the non-dynamic objects. Using the o2 and o3 objects in the assignments will trigger a different behavior: // Declaration of a new dynamic object. o4 is dynamic ClassWithPrivateConstructor  // o2 is a reference to o4 o2 = o4  // The members of o4 are copied into o3 o3 = o4
Syntax of constructors for the base classes and members Running the constructor of a base class or memberIf a base class or a class type member has a constructor, this constructor is automatically called without parameter. You must: - assign default values to the parameters of the constructor (or to the parameters of the base class or member)
- explicitly call the constructor by passing the parameters.
Calling the constructor method to build a classConstructor <ClassName>(<Parameters>) Details of syntax <ClassName>
Name identifying the class. <Parameters>
Parameters of the constructor. Calling the constructor to build a memberConstructor <Member name> (<Parameters>) Details of syntax <Member name>
Name identifying the member of the class. <Parameters>
Parameters of the constructor. ExampleThe explicit constructors of the base class or member must be called in first statement of the constructor of the derived class. //----Declare the BaseClass1 class BaseClass1 is Class BaseClass1Member is int END
//---- Constructor of BaseClass1 PROCEDURE Constructor(Param1) Info("Constructor of BaseClass1 => " + Param1)
//----Declare the BaseClass2 class BaseClass2 is Class BaseClass2Member is int END
//---- Constructor of BaseClass2 PROCEDURE Constructor(Param1) Info("Constructor of BaseClass2 => " + Param1)
//---- Declaration of DerivedClass DerivedClass is Class // Inheritance of BaseClass1 whose // Constructor expects a parameter inherits from ClassBase1 // BaseClass2 member whose // Constructor expects a parameter DerivedClassMember is BaseClass2 END
//----Constructor of DerivedClass PROCEDURE Constructor() // Explicit call to Constructor Constructor BaseClass1(10) Constructor DerivedClassMember(20)
Default value of members Each member of a class is set to zero if no constructor is associated with the class. Syntax of the Destructor method Declaration of the methodPROCEDURE Destructor <ClassName>() Details of syntax <ClassName>
Name identifying the class. The destructor accepts no parameter.
Related Examples:
|
Training (WINDEV): WD Simple OOP
[ + ] The "WD Simple OOP" example is an educational example about the OOP with WINDEV. This example presents the operating mode of: - classes, - inheritances, - virtual procedures, - UML diagrams, - ...
|
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|