I have a main class which takes 2 arguments, Two arguments(Same) are coming from 2 another classes which are doing differnet computations . Now I need to define a 3rd argument which allows user to specify which class he wish to chooses .
3 次查看(过去 30 天)
显示 更早的评论
Lest say : Main Class is : obj = mainClass (a,b) other 2 classes which uses a,b are :
1) obj = Sum(a,b) 2) obj = Diff(a,b)
I need to provide a 3rd input argument in the main class where user can decide which one out of the 2 class he wants to use, wether sum class or diff class.
How do I define 3rd argument linking the 2 classes to choose.
0 个评论
采纳的回答
Jeff Miller
2018-7-11
I'm guessing main class is a parent class and sum and diff are two child classes descended from it.
If that is right, the best approach is to write a separate function that returns whichever class the user wants, e.g.,
function myClass = ClassSelector(a,b,type)
switch type
case 'sum'
myClass = Sum(a,b);
case 'diff'
myClass = Diff(a,b);
end
end
In good OO design, the parent class should not know anything about the different child classes. For example, you don't want to have to change the parent class if you add a new type of child (e.g, Product(a,b)).
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Signal Generation and Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!