2.2 How to make a constructor for an Object?

Add ModuleCopy proc in the class. This will automatically be called to initialize the object.

Here is an example

restart; 
 
module ODE() 
 option object; 
 local ode:=NULL; 
 local y::symbol; 
 local x::symbol; 
 local sol; 
 
 export ModuleCopy::static := proc( _self::ODE, proto::ODE, ode, func, $ ) 
    print("Initilizing object with with args: ", [args]); 
    _self:-ode:= ode; 
    _self:-y:=op(0,func); 
    _self:-x:=op(1,func); 
 end proc; 
 
 export dsolve::static:=proc(_self,$) 
   _self:-sol := :-dsolve(ode,y(x)); 
 end proc; 
 
 export get_sol::static:=proc(_self,$) 
   return sol; 
 end proc; 
 
end module;
 

And now make an object and use it as follows

o:=Object(ODE, diff(y(x),x)+y(x)=sin(x), y(x)); 
o:-dsolve(): 
o:-get_sol(); 
 
        #y(x) = -1/2*cos(x) + 1/2*sin(x) + exp(-x)*_C1
 

So a constructor just makes it easier to initialize the object without having to make a number of set() calls to initialize each member data.