Overload only several functions
7 次查看(过去 30 天)
显示 更早的评论
Hi,
I am totally new to Matlab OOP. For my program to work, I need to redefine the built-in min, max and abs functions. However, if I declare these three functions as methods, all the other functions like sine, cosine, etc. must be declared so that they act on the defined object. So how can I only redefine (overload) these three functions and let the others unchanged.
Thanks, Zoli
2 个评论
Image Analyst
2014-10-18
I believe redefining is usually called overriding, not overloading which is somewhat different because with overloading you can have different functions and it figures out which to use based on what inputs you pass and outputs you accept.
采纳的回答
Guillaume
2014-10-18
It sounds like you're trying to implement a numeric class. So I'll refer you to matlab own's documentation on subclassing built-in classes
If you derive from a numeric class (e.g. double), you'll automatically get all the methods that apply to double, and you can override the one you want.
Otherwise, there's no other way than you writing all of them, even if it's just a dispatch to the built-in ones.
6 个评论
Guillaume
2014-10-20
As I said:
With matlab class system, I'm afraid there's no way around it but to override the methods in your class and just dispatch to the base class.
There's not that many so it shouldn't be too much of a hassle
更多回答(1 个)
Geoff Hayes
2014-10-18
Zoltán - have you created a new class that you wish to implement the max, min, and abs functions for? Or are you trying to overload these three functions for all data types?
If the former, then why not try something like the following
% class definition
classdef MyClass < handle
% private data members
properties (Access=public)
attribute1;
attribute2;
end
methods (Access=public)
% class constructor
function [obj] = MyClass(attr1,attr2)
obj.attribute1 = attr1;
obj.attribute2 = attr2;
end
% returns the instance of MyClass in the array with the greatest
% attribute2 property
function [maxObj] = max(myClassArray)
maxObj = myClassArray(1);
for k=2:length(myClassArray)
if maxObj.attribute2 < myClassArray(k).attribute2
maxObj = myClassArray(k);
end
end
end
% returns the instance of MyClass in the array with the smallest
% attribute2 property
function [maxObj] = min(myClassArray)
maxObj = myClassArray(1);
for k=2:length(myClassArray)
if maxObj.attribute2 > myClassArray(k).attribute2
maxObj = myClassArray(k);
end
end
end
function display(obj)
fprintf('Attribute 1 = %f Attribute 2 = %f\n',...
obj.attribute1, obj.attribute2);
end
end
end
The above definition is for a very simple class with two attributes. We define the max and min methods to determine that instance (of this class) in the input array that has the maximum attribute2 property and the minimum attribute2 respectively. For example,
myObjA = MyClass(1,2);
myObjB = MyClass(3,4);
myArray = [myObjA myObjB];
maxObj = max(myArray);
minObj = min(myArray);
In the above, we can see that maxObj corresponds to myObjB, and minObj corresponds to myObjA.
3 个评论
Geoff Hayes
2014-10-18
Try using the builtin function so we can call the MATLAB builtin function from our overloaded method.
Add the following method to the above class
% calculate the sine of attribute 1
function [result] = sin(obj)
result = builtin('sin',obj.attribute1);
end
Now, try the following
clear all;
myObjA = MyClass(pi/4,12);
sin(myObjA)
and we get the expected answer of sin(pi/4) as
ans =
0.707106781186547
As for your question concerning handle, see the abstract class for deriving handle classes for more details.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Methods 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!