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

조회 수: 9 (최근 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
fa wu
fa wu 2023년 8월 30일
Thank you for your response; your analogy was very helpful for understanding, it's great!

댓글을 달려면 로그인하십시오.

추가 답변 (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