属性如何在面向对象的 MATLAB 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/209005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:40:29  来源:igfitidea点击:

How do properties work in Object Oriented MATLAB?

matlabooppropertiesmatlab-class

提问by

I am trying to create a MATLAB class with a member variable that's being updated as a result of a method invocation, but when I try to change the property within the class it (apperently, from what I understood from MATLAB's memory management) creates a copy of the object and then modifies it, leaving the original object's property untouched.

我正在尝试创建一个带有成员变量的 MATLAB 类,该成员变量因方法调用而更新,但是当我尝试更改类中的属性时,它(显然,根据我对 MATLAB 内存管理的理解)创建了一个副本对象,然后修改它,保持原始对象的属性不变。

classdef testprop  
    properties  
        numRequests=0;  
    end  
    methods  
        function Request(this, val)  
            disp(val);  
            this.numRequests=this.numRequests+1;  
        end  
    end  
end  

.

.

>> a=testprop;
>> a.Request(9);
>> a.Request(5);  
>> a.numRequests  

ans = 0  

回答by Azim

Using a Vanilla Class

使用 Vanilla 类

When using vanilla class you need to tell Matlab to store a modified copy of the object to save the changes in the property value. So,

使用 vanilla 类时,您需要告诉 Matlab 存储对象的修改副本以保存属性值的更改。所以,

>> a=testprop
>> a.Request(5); % will NOT change the value of a.numRequests.
5

>> a.Request(5) 
5

>> a.numRequests
ans = 
       0

>> a=a.Request; % However, this will work but as you it makes a copy of variable, a.
5

>> a=a.Request; 
5

>> a.numRequests
ans =
       2

Using the Handle Class

使用句柄类

If you inherit from the handle class, that is

如果你从handle类继承,那就是

classdef testprop < handle

then you can write,

然后你可以写,

>> a.Request(5);
>> a.Request(5);
>> a.numRequests
ans = 
       2

Update: Using Vanilla Class

更新:使用 Vanilla 类

As Kamrannotes for the above to work the definition of the Requestmethod in the question's example code needs to be changed to include an output argument of type testprop.

正如Kamran 所指出的,要使上述内容起作用,Request需要更改问题示例代码中方法的定义,以包含类型testprop的输出参数。

Thanks Kamran.

谢谢卡姆兰。

回答by Marc

You have to remember that syntactically in Matlab, you're probably closer to C, than C++ or Java, at least with respect to objects. So, of you want to change the "contents" of a value object (really just a special struct), you need to return the object from the function.

您必须记住,在 Matlab 中的语法上,您可能更接近 C,而不是 C++ 或 Java,至少在对象方面。所以,如果你想改变一个值对象的“内容”(真的只是一个特殊的struct),你需要从函数中返回对象。

Azim was correct to point out that if you want Singletonbehavior (which, from your code, you seem to), you need to use a "handle" class. Instances of classes that derive from Handle all point to a single instance, and operate only on it.

Azim 正确地指出,如果您想要单例行为(从您的代码来看,您似乎是这样),您需要使用“句柄”类。从 Handle 派生的类的实例都指向单个实例,并且只对它进行操作。

You can read more about the differences between Value and Handle classes.

您可以阅读有关 Value 和 Handle 类之间差异的更多信息。

回答by Kamran Bigdely

I made the class testpropand tried to excute the code which Azim suggested but it did not work. When I executed the following command:

我创建了类testprop并尝试执行 Azim 建议的代码,但它不起作用。当我执行以下命令时:

a=a.Request(1)

The following error was generated:

产生了以下错误:

??? Error using ==> Request Too many output arguments.

???错误使用 ==> 请求太多的输出参数。

I think the problem is that we did not determine any output when declaring Requestmethod. So we should change it to:

我认为问题在于我们在声明Request方法时没有确定任何输出。所以我们应该把它改成:

function this = Request(this, val)

and now:

现在:

>> a = testprop;
>> a = a.Request(1);        
>> a.numRequests

ans = 1