how to call a get.properties method in a script

3 ビュー (過去 30 日間)
Marc-Olivier Labrecque-Goulet
Marc-Olivier Labrecque-Goulet 2017 年 3 月 23 日
回答済み: Guillaume 2017 年 3 月 23 日
Hi, I'm trying to use the get.propertie function on a private propertie.
classdef t_point2D
properties (Access = private)
X;
Y;
end
methods
%Defenir le constructeur
function point2D = t_point2D(X,Y)
if nargin == 0
point2D.X =0;
point2D.Y=0;
elseif nargin == 1 || nargin >2
fprintf('Error');
else
point2D.X = X;
point2D.Y = Y;
end
end
%accesseurs
function X = get.X(point2D)
X = point2D.X;
end
Then, I try to call it in a script :
point1 =t_point2D(2,8);
XX = get.X(point1);
I get the following error message : Undefined variable "get" or class "get.X".
Error in test (line 4) XX = get.X(point1);
I supposed I'm not calling the method the right way? I can't find the doc on how to call a get.prop function.

採用された回答

Guillaume
Guillaume 2017 年 3 月 23 日
You have two problems:
1. You're using the wrong syntax for accessing properties. Regardless of the syntax used to define the property accessor, the way to access the property is just obj.propname, so:
XX = point1.X
2. The above won't help because the whole point of a private property is that it can't be accessed from outside the class. So if you want access to the X property you need to make it public (at least for GetAccess)
Note: As shown, your accessor function does not enable anything that wasn't already available without it. So if all you do is make X public, you may as well remove that function.
The following may be what you're after:
classdef Point2D
properties (GetAccess = public, SetAcces = private)
X;
Y;
end
methods
function this = Point2D(X, Y) %constructeur
%body of constructor
end
%No need for accessor, you can already do Point2D.X
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by