Classdef and objects
Classdef and objects
Description
Define classes and objects in Scilab scripts
Definition
Classdef are used to define a set of properties and/or methods that work together. Properties are like variables and methods like functions.
classdef <classname> < <superclass> & <superclass> properties (<modifiers>) var1 = [] end methods (<modifiers>) function <classname>(args...) // constructor ... end function rets=func1(args...) ... end func2 = externalfunc end enumeration enum1(args...) enum2(args...) enum3(args...) end end
Classes are defined by :
- Name: it will be used to use class
- Superclass: names of superclass from which it derives
- Properties: variables used inside or outside class to store data
- Methods: functions to do some operations
- Enumeration: Specific instances of class
Properties and Methods can have some modifiers to change their visibility or behavior:
Private: elements can be used onlyinsidemethods of this class. (Public/Protected/Private are exclusive)Protected: elements can be usedinsidemethods of this class and its children (inheritance). (Public/Protected/Pritave are exclusive)Public(default): elements can be usedoutsideandinsideof this class. (Public/Protected/Pritave are exclusive)
inheritance
Classes can derive from one or multiple classes. (with &)
In this case, elements of the parent classes can be used in this class (depends of the modifiers)
classdef <classname> < <superclass> & <superclass> end
Properties
Define properties of class.
Visibility modifier can be private, protected or public.
A default value can be added to initialize variable during instantiation (default [])
classdef <classname> properties (<modifier>) var1 = [] end end
Methods
Define methods of class.
Visibility modifier can be private, protected or public.
In methods this must be used to access to properties or methods of the object
classdef <classname> methods (<modifier>) function rets=func(args...) this.var1 = 12; end func2 = externalfunc end end
Note: externalfunc can be a macro or a C/C++ gateway.
In C/C++ gateway, use symbol::Context::getInstance()->getCurrentObject() to get this
Emuneration
Define enumeration of class.
They are specific instances of the class associated to a name.
classdef Color enumeration RED (args...) GREEN (args...) BLUE (args...) end end //usage Color.RED Color.GREEN Color.BLUE
Overloads
Overloads will be called for undefined operations or function calls. (overloading)
There is 2 forms:External: defining an overload function
%type1_operator_type2for operators (standard overload way in Scilab)Or
%type_functionfor functionsInternal: using methods declaration in classdef
For functions, define a method with the same name (ex:
dispin following example)There is special functions called by internal mecanism like:
saveobj: call when saving an object to serialize data.prototype:
function data=saveobj(), endYou have to return a native scilab variable (struct, list, ...) except objects that contains all data you want to save.
loadobj: called when object is restore byloadfunction.prototype:
function loadobj(data), enddatais the variable returned bysaveobj.delete: called when object is clearedprototype:
function delete(), end
For operators following the lookup table :
Operations Methods to define Operations Methods to define Operations Methods to define Operations Methods to define a +bplus(a, b) +a uplus(a) a - b minus(a, b) -a uminus(a) a * b mtimes(a, b) a / b mrdivide(a, b) a \ b mldivide(a, b) a ^ b mpower(a, b) a .* b times(a, b) a ./ b rdivide(a, b) a .\ b ldivide(a, b) a .^ b power(a, b) a .*. b kron(a, b) a ./. b rkron(a, b) a .\. b lkron(a, b) a *. b controltimes(a, b) a /. b controlrdivide(a, b) a \. b controlldivide(a, b) Comparisions Methods to define Comparisions Methods to define a == b eq(a, b) a <> b ne(a, b) a < b lt(a, b) a <= b le(a, b) a > b gt(a, b) a >= b ge(a, b) a & b and(a, b) a | b or(a, b) a && b shortand(a, b) a || b shortor(a, b) Operations Methods to define Operations Methods to define Operations Methods to define ~a not(a) a' ctranspose(a) a.' transpose(a) Matrix manipulations Methods to define Matrix manipulations Methods to define [a b] horzcat(a, b) [a;b] vertcat(a, b) a(i1, i2, ...) extract(i1, i2, ...) a(i1, i2, ...) = b insert(i1, i2, ..., b) for the most part of overload, you can definie a specific one for a identitified type.
ex: insert_s (insertion of a double in the object).
generic one will be used if specific one does not exist.
Search order is: specific (insert_s), generic (insert), scilab standard (%type1_i_type2)
classdef Matrix properties value = [] end methods function Matrix(v) this.value = v; end function r=plus(a, b) //generic overload for operation + r = Matrix(a.value + b.value); end function r=plus_b(a, b) //overload for Matrix + bool or bool + Matrix if isa(a, "Matrix") then r = Matrix(a.value + double(b)); else r = Matrix(double(a) + b.value); end end function r=plus_i(a, b) //overload for Matrix + int or int + Matrix if isa(a, "Matrix") then r = Matrix(a.value + double(b)); else r = Matrix(double(a) + b.value); end end function disp() disp(this.value); end end end a = Matrix([1 2 3 4]); b = Matrix([4 3 2 1]); a + b a + %t %f + b a + int8(10) int8(10) + b
Examples
Usage of enumeration:
classdef Color properties R G B end methods function Color(varargin) s = size(varargin); select (s) case 1 c = varargin(1); [_, _, _, colors] = regexp(c, "/([0-9A-Fa-f]{1,2})([0-9A-Fa-f]{1,2})([0-9A-Fa-f]{1,2})/"); colors = hex2dec(part(colors + colors, 1:2)); this.R = colors(1); this.G = colors(2); this.B = colors(3); case 3 this.R = modulo(varargin(1), 256); this.G = modulo(varargin(2), 256); this.B = modulo(varargin(3), 256); end end function disp() printf("Color: #%02x%02x%02x\n", this.R, this.G, this.B); end end enumeration BLACK ( 0, 0, 0) BLUE ( 0, 0, 255) GREEN ( 0, 255, 0) CYAN ( 0, 255, 255) RED (255, 0, 0) MAGENTA (255, 0, 255) YELLOW (255, 255, 0) WHITE (255, 255, 255) end end red = Color.RED gray1 = Color(128, 128, 128) gray2 = Color("#808080")
See also
- overloading — возможности перегрузки отображения, функций и операторов
- properties — Get accessible properties of a classdef or an object
- methods — Get accessible methods of a classdef or an object
- enumeration — Get enumeration of a classdef or an object
History
| Версия | Описание |
| 2026.0.0 | classdef introduction. |
| Report an issue | ||
| << boolean | types | функции >> |