Create and lock a struct with fields in a class
45 次查看(过去 30 天)
显示 更早的评论
I have a class and want to define a struct in that class with given fields (I do know how to do this), but then lock the fields of that structure. So, the user of that class can change values within that field, but not the structure itself. Similar to in C++ when you define a Struct; the access to it can be public, but the user can't change what the struct looks like. I've been playing around a lot with options, but it doesn't seem possible.
Example:
classdef myclass
%UNTITLED4 Summary of this class goes here
% Detailed explanation goes here
properties
x = struct('a',0,'b',1);
end
end
I can change x.a, etc which is good. But I can also say:
tmp = my_class;
tmp.x.c = 1;
And it creates the field c which I don't want to allow. I am trying to do this to only allow the user to change fields defined by the class itself. This will avoid someone thinking they set a variable but mispelled it, for example.
My workaround is to the property protected, then write some set function that first checks if it is already a field before setting it. Thanks for any advice!
0 个评论
回答(2 个)
Matt J
2022-5-29
编辑:Matt J
2022-5-31
One way:
classdef myclass
properties
x = struct('a',0,'b',1);
end
methods
function obj=set.x(obj,x)
try
obj.x=orderfields(x,obj.x);
catch
error 'Fields of obj.x are locked'
end
end
end
end
2 个评论
Matt J
2022-5-31
编辑:Matt J
2022-5-31
Not sure if this current answer does the right thing, but the example you had in here previously (I'm pasting it in here) does work
If it does work, and meets your needs, then I would be much obliged if you would Accept-click the answer.
with the caveat that it only works when the property is a single layer struct
If you mean that only the fields of obj.x are locked (not the fields of any structures x contains), then yes. However, it can only be called a caveat if your post had made any mention of such additional requirements.
What is the full extent of the locking that you imagined? Suppose I were to change obj.x.a=1 to a different data type like obj.x.a='fish'. Would you want to forbid things like that as well?
Ralf Ritter
2022-5-31
You could also define a class for property x.
This even allows you to specify the content/type of the fields a and b, if you use property validation:
classdef X
properties
a (1,1) double
b (1,1) double
end
methods
function obj = X(a,b)
obj.a = a;
obj.b = b;
end
end
end
And your myclass would be:
classdef myclass
properties
x = X(0,1);
end
end
If you then try to change a "field" (rather property) of x:
tmp = myclass;
tmp.x.c = 1;
This results in an error:
Unrecognized property 'c' for class 'X'.
And to demonstrate one of the benefits of property validation:
tmp.x.a = 'foo'
Error setting property 'a' of class 'X'. Value must be a scalar.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!