Overload only several functions
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zoltán Csáti
am 18 Okt. 2014
Kommentiert: Zoltán Csáti
am 20 Okt. 2014
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 Kommentare
Image Analyst
am 18 Okt. 2014
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.
Akzeptierte Antwort
Guillaume
am 18 Okt. 2014
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 Kommentare
Guillaume
am 20 Okt. 2014
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
Weitere Antworten (1)
Geoff Hayes
am 18 Okt. 2014
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 Kommentare
Geoff Hayes
am 18 Okt. 2014
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.
Siehe auch
Kategorien
Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!