Add 2 Numbers in a class
87 次查看(过去 30 天)
显示 更早的评论
So I am trying to learn OOP in MATLAB. I am a beginner in MATLAB and trying to learn stuff. So I decided to create this simple addition class where it has a function that takes in 2 inputs and returns the output as addition of two numbers. Before you answer this can be done as a simple function, yes I know that, but the objective is to learn OOP. So thats why I am creating a class. And I wrote this programm.
classdef Add
%ADD Summary of this class goes here
% Detailed explanation goes here
properties(Access = private)
Value
end
methods
function obj = addNumbers(Nr1,Nr2)
%ADD Construct an instance of this class
% Detailed explanation goes here
obj.Value = Nr1 + Nr2;
end
end
end
Then in the Command Window I created an object and tried to call the function addNumbers.
x = Add;
x.addNumbers(1,2);
But the output is
Error using Add/addNumbers
Too many input arguments.
So I havent understood OOP correctly. Could you help me understand where I am wrong and correct me. Thank you.
0 个评论
采纳的回答
Raheel Naveed
2023-7-24
The constructor function (the function that has the same name as the class) should have the same name as the class.
So, it should be Add instead of addNumbers.
Try replacing it as
classdef Add
properties(Access = private)
Value
end
methods
function obj = Add(Nr1, Nr2) % Changed
obj.Value = Nr1 + Nr2;
end
function val = getValue(obj)
val = obj.Value;
end
end
end
In Command Window, or other file you may use this commands to see the output
addObj = Add(3, 5)
sumValue = addObj.getValue();
% disp(sumValue); % Displays 8
disp('The sum is '+ string(sumValue));
3 个评论
编辑:Govind Sankar Madhavan Pillai Ramachandran Nair
2023-7-25
Raheel Naveed
2023-7-25
Using a Construtor for general purpose function (like sum,power,mean etc) is more fast and effective.
Because, it does not require intialization as other functions. (I hope you know this before)
Below is the code for function other than this.
classdef Add
properties(Access = private)
Value
end
methods
function obj=addNumbers(obj,Nr1,Nr2)
obj.Value=Nr1+Nr2;
end
function val = getValue(obj)
val = obj.Value;
end
end
end
In Command Window
x=Add();
x=x.addNumbers(3,5);
sumValue=x.getValue();
disp(sumValue)
I wish you all the best in your journey of mastering OOPs.
更多回答(2 个)
Steven Lord
2023-7-24
This syntax:
x = Add;
x.addNumbers(1,2);
does not call the addNumbers method of the Add class with two inputs. It calls that method with three inputs. It is almost always equivalent to the following call (and the cases where they can differ are a bit of an advanced maneuver and aren't relevant for this discussion.)
addNumbers(x, 1, 2)
If I were to write up a class to add two numbers, rather than having the class be just effectively a delegator to a method (that has little or no interaction with the class itself) I'd write the class to represent a number. In fact the documentation has at least a few examples of this type of class. I'd recommend you read through the BasicClass example on this documentation page. Note that this class doesn't do everything you might expect; for example it would be nice to be able to add 1 and a BasicClass object without having the user explicitly convert 1 into a BasicClass object. The polynomial example on this documentation page talks about how to enable that type of mixed arithmetic operation to work via a small amount of extra work on the part of the class author.
另请参阅
类别
在 Help Center 和 File 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!