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

19 ビュー (過去 30 日間)
fa wu
fa wu 2023 年 8 月 30 日
コメント済み: fa wu 2023 年 8 月 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.
  1 件のコメント
fa wu
fa wu 2023 年 8 月 30 日
Why does the program you provided result in an error when run?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConstruct 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