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?

采纳的回答

per isakson
per isakson 2012-8-11
编辑:per isakson 2012-8-11
  1. 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.
  2. 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.
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
Steven Lord 2020-7-8
See this documentation page for a description of the difference between handle classes and value classes.
As for default arguments, perhaps function argument validation does what you want.
emre parlak
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
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 个评论
Manish Makwana
Manish Makwana 2012-8-11
Yes, obj refers to the object the method is inside. I need to use the reference else matlab creates a new variable 'Height', independent of the class property 'Height'.
Note that i have about 4 other variables that use DeltaTime, and that DeltaTime does not equal DeltaHeight. Hence why i want to save it as a property, which can be privately referenced (instead of having to expose lots of dependent properties).
I'll post further code when i get to a desktop.
Robert Jack
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 CenterFile Exchange 中查找有关 Methods 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by