class1 as property of class2, class1 property initialization?

3 次查看(过去 30 天)
I have a class:
classdef class1
properties
prop1 = 1;
end
end
and I can easily access prop1 with:
test = class1
test.prop1
ans = 1
If I then put class1 as a property into another class e.g.:
classdef mainClass
properties
class1
end
end
I would like to access class1 propr 1 through mainClass. So I try the following but get an error:
>> test.class1(1).prop1
Index exceeds the number of array elements. Index must not exceed 0.
Why is prop1 not initialized to the value 1? Thanks

采纳的回答

Benjamin Kraus
Benjamin Kraus 2022-11-1
In your definition of mainClass you created a property with the name class1, not the type class1.
This is what you want:
classdef mainClass
properties
class1 (1,1) class1
end
end
Just be careful using this approach. If class1 is a handle class, then all instances of mainClass will share the same class. In that case, you are better off with this approach:
classdef mainClass
properties
class1
end
methods
function obj = mainClass()
obj.class1 = class1;
end
end
end
You may also want to reconsider having a property name that matches a class name. It works, but it may be confusing.
  10 个评论
J G
J G 2024-1-25
I had thought of making it required in the constructor. My hesitation was more related trying to maintain a standard object setup experience across different classes used in the application. Additionally having it in the constuctor, means that that object must be instantiated prior to creation of a MyClassB object, something I would rather not impose.
I played around with the matlab.mixin.Scalar idea, but didn't get it to work (I may not have been doing it correctly, and didn't spend a lot of time).
In the end, I found a native validitation function MustBeScalarOrEmpty that does the trick, and achieves most of what I want (i.e. an empty default value, property size and class enforecement, self-documentation). See below:
classdef MyClassA
properties
Bobj (1,:) MyClassB {MustBeScalarOrEmpty} % Still provides decent self documentation
end
methods
function obj = MyClassA()
end
function output = doStuffWithB(obj,input)
if isempty(obj.Bobj) % This now will work, because default will be a 1x0 MyClassB object
error('User must first define Bobj')
else
output = obj.Bobj.myBMethod(input);
end
end
end
end
J G
J G 2024-1-25
@Benjamin Kraus, sorry! It seems that I didn't see your post prior to writing my recent comment! As you can see I ended up independently discovering the second solution you proposed using mustBeScalarOrEmpty.
I'll think more about your preffered solution. I do hear the benefits as you so eloquently spelled them out. As I mentioned, my current hesitation relates more to how the user creates objects for the application, and the fact making it a required constructor argument would impose a limitation on the sequence at which different objects must be created...

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by