How do I cast a superclass object into a subclass object in MATLAB 7.7 (R2008b)?
37 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2013 年 8 月 27 日
編集済み: MathWorks Support Team
2020 年 7 月 10 日
If I have a superclass object, and I would like to cast it into a subclass object. In the pre-R2008b class system I could do this:
obj_sub = class(struct(obj_super),'mySubClass');
In Java, I can do this as:
(mySubClass) obj_super
I do not see any straighforward way to do this in the current MATLAB class system.
採用された回答
MathWorks Support Team
2020 年 8 月 12 日
編集済み: MathWorks Support Team
2020 年 7 月 10 日
MATLAB 7.7 (R2008b) does not have any functionality for directly casting a superclass object to a subclass object.
As a workaround (in any version of MATLAB), create and use a method which automatically copies over non-dependent property values:
function output = copyObject(input, output)
C = metaclass(input);
P = C.Properties;
for k = 1:length(P)
if ~P{k}.Dependent
output.(P{k}.Name) = input.(P{k}.Name);
end
end
end
For more information about converting objects of one class to another and some examples, please refer to the following documentation page:
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Class Introspection and Metadata についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!