Azzera filtri
Azzera filtri

Wanted: enumeration class hierarchy

3 visualizzazioni (ultimi 30 giorni)
Alexander
Alexander il 16 Gen 2017
Commentato: Adam il 16 Gen 2017
I am working with an application in which objects are processed through a number of stages along one of several tracks.
Currently, I define each track as an ordinal categorical array where each element is a stage. Each object to be processed contains a scalar from one of these categorical arrays to determine which stage it is at. This allows me to use gt, lt, etc. to determine what needs to be done while still giving each stage a meaningful name.
There are a number of things that make this approach less than ideal. All tracks share the first two stages. Thus, to avoid redefining those two stages with each track, I'm forced to use "addcats," which is rather clunky. Additionally, a categorical from one track cannot be compared to one from a different track, even if they are both in the first two stages, which are shared across all tracks. Finally, ordinal categoricals do not support the colon operator.
I would like to instead create an enumerated class hierarchy, with each track being its own class, and the superclass of all the tracks being itself a subclass of one of the numeric types. This superclass would enumerate the first two stages, and each track would enumerate the rest the stages. The superclass would also overload plus, minus, and colon to work on these enumerations. Defining these as classes would also make it easier to keep track of metadata associated with each stage (currently, to make sure this metadata stays organized -- e.g. always has the same number of rows as the categorical vector -- I include the metadata as columns of a table).
Any ideas on how to get the behavior I'm looking for?
Thanks!
Superclass:
classdef (Abstract) AbstractEnum < single
enumeration
Stage1(1)
Stage2(2)
end
methods
function obj = AbstractEnum(x)
obj@single(x)
end
function x = colon(varargin)
x = AbstractEnum(builtin('colon',varargin{:}));
end
function x = plus(varargin)
x = AbstractEnum(builtin('plus',varargin{:}));
end
function x = minus(varargin)
x = AbstractEnum(builtin('minus',varargin{:}));
end
end
end
Track A:
classdef TrackA < AbstractEnum
enumeration
TrackA_Stage3(3)
TrackA_Stage4(4)
end
end
Track B:
classdef TrackB < AbstractEnum
enumeration
TrackB_Stage3(3)
TrackB_Stage4(4)
TrackB_Stage5(5)
TrackB_Stage6(6)
end
end
  1 Commento
Adam
Adam il 16 Gen 2017
An enumeration is generally a very simple class that purely defines the types of the enumeration. You can add properties and functions alongside it (I sometimes do, but not usually), but they are meant to be lightweight classes really.
Can you not just put all your behaviour in a standard class which also contains a property that is an enumeration object, then it can act on this as you desire without trying to extend it, overload functions and all sorts of other things.
Arithmetic on an enumeration is not a concept I would ever want to involve myself in either because the whole point of an enumeration is to move away from the numeric representation in favour of a more user-friendly named object/class.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Enumerations in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by