What is the purpose of non-static methods in MATLAB?

Since MATLAB does not provide self-assessment , what is the actual difference between a static and non-statistical method in MATLAB, other than the latter is not a call can be used without an instance of the class? In any case, you should always pass a reference to the object-to-be-modified ( edit , apart from detunings, getters, and overloaded operators , which implicitly include self-evaluation)

+3


source to share


2 answers


For non-static methods, Matlab provides the calling class as the first argument. By (personal convention) I invoke this argument self

, which then emulates the self-reference syntax. eg:.

methods (Static = false)
    function output = someMethod(self, arg1, arg2, arg3)
        self.x      %Now refers to the (potentially private) field `x`
        self.someOtherFunction(arg1, arg2) %Calls another method, which may be static or not.
    end
end

      

Unlike

methods (Static = true)
    function output = someStaticMethod(arg1, arg2, arg3)
        %There is no input appropriate to the name "self" 
        someOtherFunction(arg1, arg2) %Calls another method, which must be static
    end
end

      



Given an object someObject

, these methods can be called with:

someObject.someMethod(arg1, arg2, arg3)
someObject.someStaticMethod(arg1, arg2, arg3)

      


The self-regulation discussed in the related question refers to package names that are a completely different animal.

+5


source


Perhaps more relevant, static methods can be called without calling the object's constructor: for example, if the class foo has a static method bar, then foo.bar () calls the static method bar without calling the constructor foo ().



0


source







All Articles