Why should one use class and enumeration instead of structures? and what effect MATLAB will result after using class and enumeration?

12 Ansichten (letzte 30 Tage)
classdef someClass
enumeration
end
end
  2 Kommentare
Rahul Mahadik
Rahul Mahadik am 12 Aug. 2016
after defining class and enumeration, we use it in a code in such a way like a.someProperty, a.someProperty2 etc. In structures we do the same. We define structure by using struct command and use it in the same way as class and enumeration.
Adam
Adam am 12 Aug. 2016
Enumeration classes don't generally come with other properties (though they can do) so you wouldn't refer to an object of an enumeration class in that way normally you would just use them for comparison purposes in e.g. a switch statement.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam
Adam am 12 Aug. 2016
Bearbeitet: Adam am 12 Aug. 2016
I always use classes over structures as they are a lot more different in Matlab than e.g. C++.
A struct is just a christmas tree that you can hang anything off at any time you want. Very nice and convenient, but can be a nightmare for debugging, sharing code amongst others, readability, etc etc because just by looking at a piece of code you have no idea what fields are on a struct at any given moment unless it is entirely defined and limited to the scope you are looking at.
A class defines clearly what its properties are and obviously has the big advantage of wrapping methods up with those too (i.e. like a class in any language).
I would never really compare specifically an enumeration with a struct though. Where I don't use an enumeration I generally use strings instead, I have never considered a struct to be in any way an alternative to an enumeration.
You need to expand on your question though if you want an answer more tailored to what you want. You didn't ask anything at all or explain what you want t o know in the body of your question.
  2 Kommentare
Walter Roberson
Walter Roberson am 17 Aug. 2016
A class defines clearly what its propertie
I don't think I can agree. Dynamic Properties
because just by looking at a piece of code you have no idea what fields are on a struct at any given moment
Just looking at a class object you often have a pretty poor idea of what the properties are unless you read the class definition in detail. There are properties, there are hidden properties, there are methods that look like properties, the display method can result in output that gives a fairly different interpretation of the object then what is actually present in the properties, and what looks like a straight-forward assignment can end up executing code that changes dependent properties or ends up computing something that changes program state in non-obvious ways. If you want to understand what your program is doing, do away with the OOP and stick with struct.
Adam
Adam am 17 Aug. 2016
Bearbeitet: Adam am 17 Aug. 2016
Certainly I agree there are many ways to create a class that obfuscates its properties and meaning, but that is always a choice. Dynamic properties are only available if you explicitly derive from dynamicprops so it is an active decision to do that. Personally I have never found a situation in which I have used them in probably > 500 classes I have written because I simply don't see the point of them (for exactly the reasons we are discussing).
I do certainly make use of a lot of dependent properties but they still appear in the properties list at least.
I think a class is like anything that provides a lot of possibilities - there is a lot of rope to hang yourself by doing things badly as well as facility to do things that make code better.
In giving answers here I usually steer clear of resorting to OOP though as I agree with you that for people who are not familiar with it or are not sure what they are doing it is too complicated. That said, for someone familiar with Matlab and used to some OOP in other languages it is quite easily to learn - I picked it up in two weeks to a level to be able to use classes successfully in my code though am obviously still learning the many nuances over 3 years later. It is hard to judge what the motivations were from the original poster in asking the question.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by