- Are you aware of the difference between handle and value classes? From your code fragments, I conclude that you anticipate the behavior of a handle class. Thus, the class must inherit from the handle class.
- One must use some convention to distinguish between class properties and local variables. It is confusing to use the same name for a property and a local variable - to say the least. A property the shall always be referred to with the full name, obj.PropertyName, e.g. obj.Height.
Setting object properties from a class method
23 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm exploring OOP with matlab and have the following code:
% method declaration
function Update(obj, Time)
obj.DeltaTime = Time; % saves time parameter to a class-wide property
obj.Height = obj.Height + obj.DeltaHeight; % new tank height
end
DeltaTime, Height are public properties of the class this sits inside. I call the method using the following syntax:
% method call
Update(TankA, 7)
There are other methods that use DeltaTime to calculate DeltaHeight. These all work perfectly fine. However, calling this method doesn't change the DeltaTime property, nor set Height to its new value. I don't get any errors thrown when the code runs, so have no idea why its not working as I expect it to. I don't know how to pass the values to the properties as method outputs either:
% using method outputs
function [DeltaTime, Height] = Update(obj,Time)
DeltaTime = Time;
Height = obj.Height + DeltaHeight;
end
Ideally I want DeltaTime, DeltaHeight to be private properties, and not manipulated directly from a separate file. Can someone please give me an idea what could be causing this?
0 个评论
采纳的回答
per isakson
2012-8-11
编辑:per isakson
2012-8-11
Here is an example, which illustrates what I believe you try to do.
>> clear all, clear classes
>> tank_a = MyTankClass;
>> display( tank_a )
Height: 0.000000
DeltaHeight:
DeltaTime:
>> Update( tank_a, 17, 5 )
>> display( tank_a )
Height: 5.000000
DeltaHeight: 5.000000
DeltaTime: 17.000000
where
classdef MyTankClass < handle
properties ( Access = private )
Height = 0;
DeltaHeight
DeltaTime
end
methods
function Update( obj, time, height )
obj.DeltaTime = time;
obj.DeltaHeight = height;
obj.Height = obj.Height + obj.DeltaHeight;
end
function display( obj )
fprintf( 'Height: %f\nDeltaHeight: %f\nDeltaTime: %f\n' ...
, obj.Height, obj.DeltaHeight, obj.DeltaTime )
end
end
end
5 个评论
Steven Lord
2020-7-8
See this documentation page for a description of the difference between handle classes and value classes.
emre parlak
2020-7-9
i used varargin and nargins and some if else statements to do it. it is not that of a big deal i guess. thanks for the help
更多回答(1 个)
Image Analyst
2012-8-11
What is obj? Is it some other object? It's not the same object is it? If it is, why aren't you simply doing:
function Height = Update(Time)
Height = Height + Time;
end
You don't even need DeltaTime since it's the same as Time that you passed in.
2 个评论
Robert Jack
2018-4-17
I don't understand why this is the accepted answer. In my view the answer by per isakson is better. I'm troubled why an mvp is asking what 'obj' is when the context is clearly that of class properties.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Methods 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!