Access to parent object or problem with organize class

11 次查看(过去 30 天)
Hi,
I need to attache some methods to the particular object obj. These methods stored to packages (folder with "+" in name).
Example: I create two instance of class: A and B. A require function from package +one, B require function from package +two. When I use this functions it is not time to clarify which package i need to use. It must be defined contemporaneously with construction of object. In additional, I don't want to write a lot of methods in main class. It is not right and not logical to write these methods in main class.
I think this problem can be solved like this:
obj.prop.met(a,b,c);
obj- object 1 (main class);
prop - property of obj;
anObj - another object ( obj.prop = anObj, class of this object contains methods which can access to function in packages);
met(a,b,c) - method of anObj (need obj to work);
Can method met(a,b,c) access to obj ? I tried to find answer but not found.
Can I use adress " this " to access to obj ?
What do you think about this problem? How you solve it?
Thanks for help. Sorry for my english:(
  2 个评论
Adam
Adam 2017-1-27
I think it would be best to try to replicate as simple an example as you can and try it out (or post here). I am not really following what you are trying to do. No need to worry about your English, but it does help to try to give code examples since they are a more universal language.
When I want to try out if some fundamental operation is possible in a class setup I just create the two or three or however many classes I need to minimally test it out without anything extra in them.
Sergey Kasyanov
Sergey Kasyanov 2017-1-27
I don't know how I can try to access to obj in methods of anObj. It brough me here.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2017-1-27
Let me see if I understand. To simplify the problem, you have two different classes, object1 and object2.
classdef object1
methods
function methodThatNeedsParent(obj)
% Some code that accesses the dieroll property from an object2
end
end
end
One of the properties of object2 contains an instance of object1.
classdef object2
properties
x
dieroll
end
methods
function obj = object2(instanceOfObject2)
obj.x = instanceOfObject2;
obj.dieroll = randi(6);
end
end
end
You want to construct an instance of object2 (call it in2), pass the instance of object1 (call it in1) contained in in2's property into a function or method, and have that function access properties of in2 via in1?
in1 = object1;
in2 = object2(in1);
methodThatNeedsParent(in2.x)
For value objects, I don't think you can do this. There's no guarantee, after all, that an instance of object1 is contained in an instance of object2. In fact, there is one in this example.
methodThatNeedsParent(in1)
If your objects were handle objects and each had a property that referred to the other (like the common graphics object properties Parent and Children as referenced in this other Answer) then you could use those properties to go back and forth. You'd want to account for empty Parent and/or Children properties -- see the properties of groot if there are no open figure windows.
  3 个评论
Steven Lord
Steven Lord 2017-1-27
For a handle class? See the attached for a VERY bare bones example.
% Create two instances of object1 (the child)
in1 = object1;
in2 = object1;
% Create one instance of object2 (the parent)
% This instance tells in1 that its parent is in3
in3 = object2(in1);
% Note that in1's Parent property is nonempty
disp(in1)
% in2's Parent property is empty
disp(in2)
% in3's Children property is in1
disp(in3)
isequal(in3.Children, in1) % true
isequal(in3.Children, in2) % false
% These calls will work and refer to in3's dieroll property
methodThatNeedsParent(in1)
methodThatNeedsParent(in3.Children)
% This call will error since in2 has no Parent
methodThatNeedsParent(in2)
There's other stuff you'd probably need or want to do (break the parent/child relationship if one is deleted, for instance, or ensure that the Parent and Children properties can only accept instances of the correct classes or classes with the correct properties) but this shows the basic idea.
Sergey Kasyanov
Sergey Kasyanov 2017-1-28
编辑:Sergey Kasyanov 2017-1-28
Thasks a lot!
It's right decision and starting point to work. I will try to avoid handle object2 class and combine more classes to solve the problem.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by