Getting a field in a Class
조회 수: 6 (최근 30일)
이전 댓글 표시
I am trying to define a Class. When I create an object of the Class, I want to be able to get one of its properties. I have made it so when I have a 'get' method I can return the property when I use the syntax:
b = newobject(4); % My class only needs to take one int value
val = get(b, 'SomeVal') % this just returns a value
How do I make make my Class so that when I write:
val = b.SomeVal
I get the same result?
Also, this dynamic preview is really slow, is there a way to disable it?
Hmm, having trouble posting the class that I made. It's very poorand I am going to move on and come back to it later
댓글 수: 2
Walter Roberson
2011년 8월 29일
Sorry, there is no way to disable the dynamic preview of the Answers system. The problem is known, and I do not know why it is taking so long to fix.
Daniel Shub
2011년 8월 31일
Can you post some more code about your class. Is your class a subclass of hgsetget? How have you defined get and how have you defined the property SomeVal? What do you currently get when you do b.SomeVal?
답변 (1개)
Lucas García
2011년 8월 29일
You should now be able to do:
b.get('SomeVal')
Or your access method for the SomeVal property has to be defined as public:
properties (GetAccess = public, SetAccess = private)
SomeVal
end
and at some point in the constructor method do:
function obj = newobject(value)
obj.SomeVal(end+1) = value; % end+1 to make it vector based
end
Then you will be able to do:
b.SomeVal
댓글 수: 9
Daniel Shub
2011년 9월 1일
Something is wrong. First, I am pretty sure outp.Value is not a valid property name and that property names cannot have "." in them. Second, you do not need a subsref method to do this. Third, your subsref method is so simple that it is going to cause all sorts of problems as you expand the class. I would suggest posting your entire simplified class file with all the methods.
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!