Object oriented programming: Difference between revisions

no edit summary
(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


657

edits