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

20 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2023-8-30
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".

更多回答(1 个)

recent works
recent works 2023-8-30
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.

类别

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

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by