<< boolean types functions >>

Scilab Help >> Scilab > types > Classdef and objects

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 :

Properties and Methods can have some modifiers to change their visibility or behavior:

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:
classdef Matrix
    properties
        value = []
    end
    methods
        function Matrix(v)
            this.value = v;
        end

        function r=plus(a, b) //generic overload for operation +
            if isa(a, "Matrix") & isa(b, "Matrix") then
                r = Matrix(a.value + b.value);
            else
                error(sprintf("Operation + not defined for %s and %s.\n", typeof(a), typeof(b)));
            end
        end

        function r=plus_s(a, b) //overload for Matrix + double or double + Matrix
            if isa(a, "Matrix") then
                r = Matrix(a.value + b);
            else
                r = Matrix(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 + 10
10 + 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

History

VersionDescription
2026.0.0 classdef introduction.

Report an issue
<< boolean types functions >>