How to make switch case in between different classes in matlab?
古いコメントを表示
Hi guys,
I have a small question regarding switch case. Iam doing my project using Matlab OOP. Is it possible to make switch case using classes? for example
clMatcore is my super class and clSteel clNano and clIron are my subclasses. I created all the classes and functions for them. Is it possible to make a switch case between them. I was a bit confused in this. Could someone help me out if possible with a small example.
Thankyou
2 件のコメント
Steven Lord
2020 年 7 月 8 日
It's not clear to me what your goal is in using a "switch case using classes". Can you describe (in words not code) what your goal is in selecting between steel, nano, and iron? What problem are you trying to solve?
kanuri venkata mohana
2020 年 7 月 8 日
回答 (1 件)
Steven Lord
2020 年 7 月 9 日
Something like:
switch material
case "steel"
x = clSteel;
case "iron"
x = clIron;
otherwise
error("I don't know how to make a transformer core out of " + material)
end
5 件のコメント
kanuri venkata mohana
2020 年 7 月 23 日
編集済み: kanuri venkata mohana
2020 年 7 月 23 日
Steven Lord
2020 年 7 月 23 日
編集済み: Steven Lord
2020 年 7 月 23 日
Write a function makeMaterial, something like:
function x = makeMaterial(material)
switch material
case "air"
x = clAircore;
case "steel"
x = clSisteel;
% etc with the other cases
otherwise
error("I don't know how to make a transformer core out of " + material)
end
If your class names had a pattern, always "cl" followed by the material followed by "core", you could do something like this that would be able to recognize new subclasses using class introspection (untested):
function x = makeMaterial(material)
subclassname = "cl" + material + "core";
S = meta.class.fromName(subclassname);
if S < ?clmaterialcore % it is a subclass of clmaterialcore
constructor = str2func(subclassname);
x = constructor();
else
error("I don't know what material " + material + " is.")
end
In your class diagram, you have a lot of inheritance, some of which seems a bit off to me. For instance, can a clmaterialcore object be substituted for a clCore object or does a clCore have a clmaterialcore? Do you want composition or inheritance?
kanuri venkata mohana
2020 年 7 月 23 日
Steven Lord
2020 年 7 月 23 日
Inheritance doesn't seem to be the right approach for some of the classes in your hierarchy.
If I were to plug in a clmaterialcore into a function that expects a clCore, should that function work? If not, clmaterialcore should not be a subclass of clCore.
If I were to plug in a clCore into a function that expects a clmaterialcore, should that function work? If not, clCore should not be a subclass of clmaterialcore.
To me, a mental model where a clCore has a clmaterialcore as one of its properties seems more natural.
classdef clCore
properties
composition % what the core is made of
end
end
Use as:
s = clSteel;
% Build a core.
C = clCore(whateverInputsAreNeededToBuildAclCore);
% Make the core out of steel.
% You probably want to specify this in the constructor call.
C.composition = s;
% Have the core ask its material about the material's conductivity.
C.composition.conductivity
kanuri venkata mohana
2020 年 7 月 23 日
カテゴリ
ヘルプ センター および File Exchange で Construct and Work with Object Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
