MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   Matlab复制构造函数 (https://www.labfans.com/bbs/showthread.php?t=22563)

poster 2019-12-07 23:17

Matlab复制构造函数
 
除了添加带有一个输入的构造函数并显式复制其属性以外,还有一种更好的方法可以为句柄派生类为matlab实现复制构造函数吗?

obj.property1 = from.property1; obj.property2 = from.property2; 等等

谢谢丹妮

回答:
如果您想要一个快速且脏的解决方案(假定可以复制所有属性),请看一下PROPERTIES函数。这是一个自动复制所有属性的类的示例:

classdef Foo < handle properties a = 1; end methods function F=Foo(rhs) if nargin==0 % default constructor Fa = rand(1); else % copy constructor fns = properties(rhs); for i=1:length(fns) F.(fns{i}) = rhs.(fns{i}); end end end end end 和一些测试代码:

f = Foo(); [fa Foo(f).a] % should print 2 floats with the same value.


所有时间均为北京时间。现在的时间是 23:27

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.