Functions or classes...............

22 visualizaciones (últimos 30 días)
shane watson
shane watson el 16 de Mzo. de 2021
Respondida: Walter Roberson el 24 de Mzo. de 2021
Hello everyboday.
Firstly, I appologise for such a basic question, I'm doing some basic coding things, however, I'm not sure about few very basic things 1) what is the technical differences while coding in functions or classes, 2) At professional level, what progammers used to do and why? 3) when I was using classes for the first time my techer told me do not change the basic syntax like properties or methods just write down your functions, so I didn't get it why do we use classes then ?and what are the meaning of properties or methods ? 4) Lastly, should use classes or functions.
Thanks in advance.
classdef untitled
%UNTITLED Summary of this class goes here
% Detailed explanation goes here %%%% what kind of explanation or summary?
properties
Property1 %%% what is this for?
end
methods
function obj = untitled(inputArg1,inputArg2)
%UNTITLED Construct an instance of this class
% Detailed explanation goes here
obj.Property1 = inputArg1 + inputArg2;
end
end
end
  3 comentarios
Adam Danz
Adam Danz el 19 de Mzo. de 2021
Looks like Jan hit the nail on the head.
Here are some additional references from the documentation that may be helpful
Perhaps your teacher gave you those instructions to keep the assignment simple and contained or to make grading easier.
shane watson
shane watson el 23 de Mzo. de 2021
Thanks btw.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 19 de Mzo. de 2021
1) what is the technical differences while coding in functions or classes
You can write a book over object oriented programming versus functions. So this question is far too general to be answered here.
2) At professional level, what progammers used to do and why?
In Matlab OOP code is slower than functions from the view point of run time. OOP has its benefits, when you create large programs in a team. Then the encapsulating of the classes and properties avoid collisions between different parts of the code.
3) [...] so I didn't get it why do we use classes then ?
I cannot answer this.
4) Lastly, should use classes or functions.
If you have to solve a homework, ask your teacher. If you want to solve your own problem: do, what solves the problem more efficiently considering the time for processing, programming and debugging.

Más respuestas (1)

Walter Roberson
Walter Roberson el 24 de Mzo. de 2021
When you have a function for a purpose, it needs to check its input parameters and internally delegate to code specific to the situation implied by that kind of data combination. This gets awkward to extend, potentially requiring that many calls to the function be re-tested to ensure that a bug has not been introduced in handling the new variety of data.
For example if all you had was a single plus() function, then when Mathworks wanted to make string + string mean concatenation, then Mathworks would have had to edit the single plus() function, go into a large structure of data checks and add cases (such as plus(string,int16) and plus(sym,string))
On the other hand, when you create classes, you can define the plus() operation for that class, knowing that at least one of the input operands will be the datatype of the class involved. This reduces code complexity a lot, and makes it easy to add new types.

Categorías

Más información sobre Construct and Work with Object Arrays 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