필터 지우기
필터 지우기

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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by