对于非静态方法,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)
链接问题中讨论的自我引用是指包名,这是一种完全不同的动物.
| 归档时间: |
|
| 查看次数: |
2954 次 |
| 最近记录: |