MATLAB-重载赋值运算符 [英] MATLAB - overload assignment operator
问题描述
假设我有以下课程:
classdef myClass < handle
properties
A = [10 20 30 40]
end
end
然后假设我有以下电话:
Then suppose I have the following calls:
>> m = myClass;
>> n = m;
当然,
n
将是与m
相同的对象的句柄.有没有一种方法可以重载赋值运算符?特别是,我想知道是否可以使用以下方法:
n
will be a handle to the same object as m
is, of course. Is there a way to overload the assignment operator? In particular, I wonder if I can have something like the following method:
function val = assign(obj)
val = obj.A;
end
因此,执行n = m
的行为与n = m.A
相同.
So doing n = m
would act the same as n = m.A
.
推荐答案
我不认为您可以:想象一下如何将对象本身分配给变量,这是完全相同的语法.而且,由于您对程序的可执行性没有任何歧义,因此它不可能且不会有用.
I don't think you can: just imagine how you would assign the object itself to a variable, it would be the exact same syntax. And since you can't have ambiguities for a program to be executable, it is very unlikely to be possible and useful.
如果您只想将其用作语法糖,请学习如何克服局限性.否则,您可能想再看一下您的设计.
If you'd just want it as syntactic sugar, learn to live with the limitations. Otherwise you might want to take another look at your design.
这篇关于MATLAB-重载赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!