OOP/Class Questions: deepcopy & AbortSet

4 ビュー (過去 30 日間)
Peter Cook
Peter Cook 2018 年 4 月 3 日
回答済み: Guillaume 2018 年 4 月 3 日
1. What is the correct way to deepcopy an instance of a class/object whose superclass is type `handle`? `copyobj` doesn't seem to work for a handle-derived class that isn't strictly a graphics object, and overloading `copyobj` as a class method to copy each field into a new instance and return a handle seems to take just as long as constructing a new object.
2. Does it make sense to specify `AbortSet` for a `Dependent` class property? Yes, I understand you can't `set` a `Dependent` property but I have defined an overloaded `set` method to access a private class property that can be set. Does it make most sense to only specify `AbortSet` for the private class property it accesses? i.e.
properties(Dependent, AbortSet)
...
%display settings
filterTimeBand
...
end
properties(Access = private, AbortSet)
...
privateFilterTimeBand = 30;
...
end
...
function propval = get.filterTimeBand(hObj)
%return the number of points used in the 2D DTS filter time
%band (x-direction / columns)
propval = hObj.privateFilterTimeBand;
end
function set.filterTimeBand(hObj,nCol)
%update the number of points used in the 2D DTS filter time
%band (x-direction / columns)
if isnumeric(nCol) && nCol>0
%first update the filter time band
hObj.privateFilterTimeBand = ceil(nCol);
%next re-filter temperature using the new filter window
hObj.filterDtsData;
end
end

採用された回答

Guillaume
Guillaume 2018 年 4 月 3 日
1. Deriving from matlab.mixin.copyable is the easiest. It's a bit of work to implement but it provides a well defined interface.
2. AbortSet on a dependent property is pointless since it'll never have a PreSet or PostSet event nor will it ever be set. I would assume that matlab just ignore the attribute for dependent properties but just in case, I would not include it.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by