Object oriented programming: Difference between revisions

From Octave
Jump to navigation Jump to search
(Created page with "GNU/Octave has support for object oriented programming. Below you can find some tips and tricks. == Making all fields public == Fields of an Octave object are by default priv...")
 
No edit summary
Line 1: Line 1:
GNU/Octave has support for object oriented programming. Below you can find some tips and tricks.
GNU/Octave has support for object oriented programming. Below you can find some tips and tricks.


== Making all fields public ==
== Creating classes ==
To create a class you need to make a folder prefixed with the symbol '''@'''. Inside the folder you need to create the ''constructor'' which is a function with the same name as the folder. For example, a class called ''dummy'' will be created in the folder ''@dummy'' and inside we should find a function (a method) ''dummy.m''. The minimal content of this method is
{{Code|A dummy class|<syntaxhighlight lang="matlab" style="font-size:13px">
function obj = dummy ()
 
  obj = struct();
 
  obj = class (obj, "dummy");
 
endfunction
</syntaxhighlight>}}
 
=== Making all fields public ===
Fields of an Octave object are by default private. This means that only methods of the object can access the field directly. For a user to access a given field directly, a method must be provided (sometimes called a ''getter'' method). If such methods is not provided, a user cannot retrieve the contents of the field. There is a work around: we can get the internal structure representing and object and from there access all the fields. The next example creates a ftp object and retrieves all the fields
Fields of an Octave object are by default private. This means that only methods of the object can access the field directly. For a user to access a given field directly, a method must be provided (sometimes called a ''getter'' method). If such methods is not provided, a user cannot retrieve the contents of the field. There is a work around: we can get the internal structure representing and object and from there access all the fields. The next example creates a ftp object and retrieves all the fields



Revision as of 20:40, 27 April 2012

GNU/Octave has support for object oriented programming. Below you can find some tips and tricks.

Creating classes

To create a class you need to make a folder prefixed with the symbol @. Inside the folder you need to create the constructor which is a function with the same name as the folder. For example, a class called dummy will be created in the folder @dummy and inside we should find a function (a method) dummy.m. The minimal content of this method is

Code: A dummy class
function obj = dummy ()

  obj = struct();

  obj = class (obj, "dummy");

endfunction

Making all fields public

Fields of an Octave object are by default private. This means that only methods of the object can access the field directly. For a user to access a given field directly, a method must be provided (sometimes called a getter method). If such methods is not provided, a user cannot retrieve the contents of the field. There is a work around: we can get the internal structure representing and object and from there access all the fields. The next example creates a ftp object and retrieves all the fields

Code: Retrieving underlying structure an object
an_object = ftp("ftp.gnu.org/gnu/");
structure = struct(an_object);

From there you can get the contents of any of the fields. Although effective, this is not very elegant. The second option, and without destroying the object oriented philosophy is to provide a general get method. Find below a working example

Code: A general get method
function answ = get (obj,varargin)

  accepted = fieldnames (obj);

  tf = ismember (tolower (accepted), tolower (varargin));

  retrieve = {accepted{tf}};
  n = numel (retrieve);
  if n == 1
    answ = obj.(retrieve{1});
  else
    for i=1:n
      answ.(retrieve{i}) = obj.(retrieve{i});
    end
  end

  unknown = {varargin{!ismember (varargin,accepted)}};
  cellfun (@(x) warning ("Unknown field %s\n", x) , unknown);

endfunction

Any object with this method has all the fields accessible by the user. They only need to know the name of the field and they can retrieve it. This method effectively makes all the fields of your object public.

Packages using object oriented programming