What is the purpose of an abstract property in MATLAB?

We have an attribute of abstractmethods and properties in MATLAB R2014b, and I know the purpose of the attribute abstractfor methods. We can call functions in this method and define it in the superclass of the class. I am confused - this is the attribute target abstractfor a property in MATLAB. How do we use it?

+4
source share
3 answers

The purpose of abstract properties (and abstract methods) is to allow the creation of interfaces :

The main idea of ​​an interface class is to specify the properties and methods that each subclass should implement without defining the actual implementation.

, Car

classdef (Abstract) Car
    properties(Abstract) % Initialization is not allowed
      model
      manufacturer
    end
end

model manufacturer ( ), , Car , . , . , " , () , model manufacturer, ".

,

classdef FirstEveryManCar < Car
    properties
      model = 'T';
      manufacturer = 'Ford';
    end
end

, ( , ).

+3

setter/getter (.. set.Property get.Property).

, Matlab, setter/getter , . , , , / getter, Abstract , setter/getter.

1 ( )

Superclass

classdef (Abstract) TestClass1 < handle
    properties
        Prop
    end
end

classdef TestClass2 < TestClass1
    methods
        function obj = TestClass2(PropVal)
            if nargin>0
                obj.Prop = PropVal;
            end
        end

        function set.Prop(obj, val)
            if ~isnumeric(val)
                error('Not a number!');
            end
            obj.Prop = val;
        end
    end
end

2 ( )

Superclass

classdef (Abstract) TestClass1 < handle

    properties (Abstract)
        Prop
    end
end

classdef TestClass2 < TestClass1
    properties
        Prop
    end
    methods
        function obj = TestClass2(PropVal)
            if nargin>0
                obj.Prop = PropVal;
            end
        end
        function set.Prop(obj, val)
            if ~isnumeric(val)
                error('Not a number!');
            end
            obj.Prop = val;
        end
    end
end
+2

, , , . , welcome , welcomeGer , :

%welcome.m
classdef welcome
    properties(Abstract)
        text
    end
    methods
        function printText(obj)
            disp(obj.text)
        end

    end
end

%welcomeGer.m
classdef welcomeGer<welcome
    properties
        text='Willkommen in unserem Hotel'
    end

end

An alternative is to skip the definition altogether text, but then Matlab will not throw an error when you forget to initializetext

+1
source

All Articles