Borrar filtros
Borrar filtros

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 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Jeff Miller
Jeff Miller el 11 de Jul. de 2018
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)).

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by