MATLAB中非静态方法的目的是什么?

Tob*_*ler 3 oop matlab static

由于MATLAB 不提供自引用,在MATLAB中静态和非静态方法之间的实际区别是什么,除了后者在没有类实例的情况下不可调用?总是必须传递对待修改对象的引用(编辑除了setter,getter和重载操作符,其中隐含地包含自引用)

Pur*_*uit 5

对于非静态方法,Matlab提供了调用类作为第一个参数.通过(个人惯例)我调用这个参数self,然后模拟自我引用语法.例如:

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
Run Code Online (Sandbox Code Playgroud)

相比之下

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
Run Code Online (Sandbox Code Playgroud)

给定一个对象someObject,可以使用以下方法调用这些方法:

someObject.someMethod(arg1, arg2, arg3)
someObject.someStaticMethod(arg1, arg2, arg3)
Run Code Online (Sandbox Code Playgroud)

链接问题中讨论的自我引用是指包名,这是一种完全不同的动物.