フィルターのクリア

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 .

2 ビュー (過去 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.

採用された回答

Jeff Miller
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 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by