How can I access the properties of A class in B class without creating objects?

20 views (last 30 days)
Here is the sample , PropertyAccess allow ClassA get property ‘Prop1’,But you must creat a PropertyAccess object in Class A. This raises an issue as creating objects may consume a significant amount of memory (assuming PropertyAccess has a large number of properties, with one property being a 100000*100000 matrix). Therefore, is there a way to get the properties ‘Prop1’ of PropertyAccess in ClassA without creating objects
classdef PropertyAccess
properties (GetAccess = {?ClassA, ?ClassB}, SetAccess = private)
Prop1=5;
end
properties (Access = ?ClassC)
Prop2=8;
end
methods (Static)
function fun()
X=PropertyAccess;
disp(X.Prop1);
end
end
end
you must creat a PropertyAccess object in Class A.
classdef ClassA < handle
properties (SetObservable) %或(SetObservable=true)
x=1;
y;
end
events
kk
end
methods
function obj=ClassA()
obj.x=1;
k=PropertyAccess(); %you must creat a PropertyAccess object in Class A.
obj.y=k.Prop1;
end
end
end

Accepted Answer

Steven Lord
Steven Lord on 30 Aug 2023
Therefore, is there a way to get the properties ‘Prop1’ of PropertyAccess in ClassA without creating objects
If Prop1 were a Constant property of the PropertyAccess class, yes. You'd access that Constant property using the name of the class rather than an instance. But since in your example Prop1 is not Constant, you must have a specific instance of PropertyAccess whose Prop1 property you access.
It's like asking what's the Name property of the HumanBeing class. That question doesn't make sense unless you ask it on an instance of HumanBeing. The Name of the instance of HumanBeing representing me is "Steve".

More Answers (1)

recent works
recent works on 30 Aug 2023
Yes, there is a way to get the properties of PropertyAccess in ClassA without creating objects. You can do this by using the matlab.mixin.SetGet class. The matlab.mixin.SetGet class provides a set of methods for getting and setting the properties of an object. To use the matlab.mixin.SetGet class, you need to make your class inherit from it.
Here is the modified code:
classdef PropertyAccess
properties (GetAccess = {?ClassA, ?ClassB}, SetAccess = private)
Prop1=5;
end
properties (Access = ?ClassC)
Prop2=8;
end
methods (Static)
function fun()
X=PropertyAccess;
disp(X.Prop1);
end
end
end
classdef ClassA < matlab.mixin.SetGet
properties (SetObservable) %或(SetObservable=true)
x=1;
y;
end
events
kk
end
methods
function obj=ClassA()
obj.x=1;
obj.y=PropertyAccess.Prop1; %No need to create PropertyAccess object
end
end
end
In this code, the class ClassA inherits from the matlab.mixin.SetGet class. This allows the class to access the properties of PropertyAccess without creating an object of PropertyAccess.
To get the property Prop1 of PropertyAccess, you can simply use the dot notation, as shown in the code.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!