为什么某些Matlab类方法需要“显然"的方法?不必要的输出参数 [英] Why do some Matlab class methods require "apparently" unnecessary output argument

查看:97
本文介绍了为什么某些Matlab类方法需要“显然"的方法?不必要的输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几个月的项目代码改进,我终于满足了定义一个新类的需要.我不得不遍历我以前的类定义作为约定的更新,我注意到所有构造函数和属性设置器都具有输出参数,即使没有分配任何参数,例如:

After evolving my project code for months, I've finally hit a need to define a new class. Having to romp through my previous class definitions as a refresher of the conventions, I noticed that all constructors and property setters all have an output argument, even though nothing is assigned to it, e.g.:

function o = myConstructor( arg1, arg2, ... )
function o = set.SomeProperty( o, arg1 )

我已经浏览了一个多小时的文档,却没有找到解释.看起来这并不取决于是在类定义文件中还是在其自己的单独的m文件中定义了函数.

I've been looking through the documentation for upward of an hour without finding the explanation for this. It doesn't look like it depends on whether a function is defined in the class definition file or in its own separate m-file.

有人可以解释吗?

推荐答案

最好的起点是文档.从最顶部开始:

The best place to start is the documentation "Comparison of Handle and Value Classes". From the very top:

value 类的构造函数返回与分配给它的变量相关联的对象.如果您重新分配此变量,MATLAB®将创建原始对象的独立副本.如果将此变量传递给函数进行修改,则该函数必须将修改后的对象作为输出参数返回.

A value class constructor returns an object that is associated with the variable to which it is assigned. If you reassign this variable, MATLAB® creates an independent copy of the original object. If you pass this variable to a function to modify it, the function must return the modified object as an output argument.

handle 类构造函数返回一个handle对象,该对象是对所创建对象的引用.您可以将句柄对象分配给多个变量,也可以将其传递给函数,而不会导致MATLAB复制原始对象.修改作为输入参数传递的句柄对象的函数不需要返回该对象.

A handle class constructor returns a handle object that is a reference to the object created. You can assign the handle object to multiple variables or pass it to functions without causing MATLAB to make a copy of the original object. A function that modifies a handle object passed as an input argument does not need to return the object.

换句话说,值类需要返回一个修改后的对象(这是一个与原始对象不同的新对象),而句柄类则不需要.这两个类的构造函数都必须始终返回一个对象,因为它实际上是在构造 .

In other words, value classes need to return a modified object (which is a new object distinct from the original), while handle classes don't. The constructor of either class will always have to return an object, since it is actually constructing it.

使用哪种类型的类" ,它链接到每种类型的类对象的几个有用的示例.查看 DocPolynom值类示例 ,您可以看到属性set方法必须返回修改后的对象,而 dlnode句柄类示例仅需要其构造函数的输出.请注意,您仍可以从句柄类方法返回一个对象(如果需要),但是它不是必需的.

Some good additional reading is "Which Kind of Class to Use", which links to a couple helpful examples of each type of class object. Looking at the DocPolynom value class example, you can see that property set methods have to return the modified object, while the dlnode handle class example only requires an output for its constructor. Note that you could still return an object from a handle class method (if desired), but it's not required.

这篇关于为什么某些Matlab类方法需要“显然"的方法?不必要的输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆