Empty objects not calling get method?

11 ビュー (過去 30 日間)
David
David 2015 年 9 月 3 日
編集済み: David 2015 年 9 月 9 日
Hi,
I have a problem regarding object oriented programming in Matlab: If I have an empty object, i.e. myObject.empty I can call methods of it and in the method manage my output for the case of an empty object, for example returning an empty array. This makes downstream code easy to handle:
obj = myObj.empty
values = obj.doCalculations(1,2)
% Inside the doCalculation(obj, input1, input2) definition I can take care of returning an empty array in case isempty(obj)
if ~isempty(values)
% proceed ...
else
% handle empty cases
end
Now my problem starts with accessing properties. If I try to access the property of an empty object it just won't return anything, not an empty array, nothing the statement:
values = obj.propertyA
will just result in an error. Making my handling of downstream code more complicated. Of course I can use try and catch, but then I also have to check which error did exactly happen and so on.But what bugs me most is that in this case the getter
get.propertyA(obj)
Does not seem to be called. So there is no way to take care of this behaviour inside the class. I feel this is a strange behaviour that I can call the methods of an empty class but not its get method? Can anybody explain me why and what would be the best way to handle these cases? I know I can check first if the object is empty but since I have a lot of linked objects this is also non trivial:
values = objA.subObjB.subObjC.property1
Edit: In case it matters I'm working solemnly with handle derived classes.

採用された回答

Matt Cohen
Matt Cohen 2015 年 9 月 8 日
Hi David,
I understand you are encountering an issue when trying to access property values of an empty object.
When creating classes in MATLAB, the default value for properties is the empty array ('[]'). It sounds like your class constructor can handle different numbers of input arguments. If this is the case, then you might be using 'varargin' as the constructor input argument so that you can handle the different number of inputs. I have included a sample class below that handles no inputs and two inputs:
classdef example < handle
properties
a
b
end
methods
function obj = example(varargin)
if nargin == 2
obj.a = varargin{1};
obj.b = varargin{2};
end
end
function set.a(obj,val)
obj.a = val;
end
function out = get.a(obj)
out = obj.a;
end
function set.b(obj,val)
obj.b = val;
end
function out = get.b(obj)
out = obj.b;
end
function out = dummy_sum(obj)
out = obj.a + obj.b;
end
end
end
With this class, I can perform the following commands and get the displayed outputs:
>> obj1 = example()
obj1 =
example with properties:
a: []
b: []
>> obj2 = example(1,2)
obj2 =
example with properties:
a: 1
b: 2
>> obj1.dummy_sum()
ans =
[]
>> obj2.dummy_sum()
ans =
3
>> obj1.a
ans =
[]
>> obj2.a
ans =
1
In this example, I do not encounter the same issue you are having with accessing a property value of an empty object. One thing that might be causing some issues is the syntax you are using for creating the empty object: myObject.empty. I am not sure if this is pseudo-code or the actual syntax you are using; if this is how you are actually instantiating the empty object, it might not be handling the property initialization properly. You might want to try a similar format to the example above, using the 'varargin' input to handle multiple forms of object instantiations.
I hope this helps.
Matt Cohen
  1 件のコメント
David
David 2015 年 9 月 9 日
編集済み: David 2015 年 9 月 9 日
You're totally right. My approach to instantiating my array was wrong. I wanted to use an empty object array since then I can easily check with isempty(obj) but then there are no real objects. The syntax to access an object is still valid but since there is no object, it doesn't return any values. And yes of course I use the varargin approach to instantiate objects and I can instantiate without any inputs. .empty is by the way is valid syntax: .empty syntax

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by