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

22 次查看(过去 30 天)
shane watson
shane watson 2021-3-16
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 个评论
Adam Danz
Adam Danz 2021-3-19
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.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-3-19
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.

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-3-24
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.

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by