How can i call class from a function?

70 次查看(过去 30 天)
Hello,
i have a class with private properties and public methods to change the properties.
I want to call the methods of the class from a function in another m.File and change the methods.
At the end a function display should return a value. Something like this:
value=dispspeed(classcar);
value=10;
How can it be done?
Thank you in advance!
  2 个评论
Rik
Rik 2018-6-29
Don't you mean something like this?
value=CarClassObject.dispspeed;
fprintf('The speed of the car is %.0f\n',value)
Or do you want to change the class itself and modify the method for all objects of that class?
Also, you should mark answers as accepted answer if they solved your problem. It is an easy way to say thank you to people investing their time in your problem.
Michael Simonovski
Michael Simonovski 2018-6-29
编辑:Michael Simonovski 2018-6-29
The function should call a constructor of the class. The function is not a part of this class!
The constructor has three inputs, all another values are set to zero. At the end a function of the class should be called to return values, not to print it. Something like {//... return value; } in C++. For example: value=dispspeed(classcar); a=10; value+a; ->100; How the method functionbody should look like?

请先登录,再进行评论。

采纳的回答

Rik
Rik 2018-6-29
You should really have a look at the documentation of how you write classes in Matlab, starting for example here.
With the code below in a file called 'car.m', you can call aCar=car(3);aCar.showSpeed and it will disp the speed.
classdef car
properties
speed
end
methods
function obj = car(set_speed)
if nargin>0
obj.speed=set_speed;
else
obj.speed=0;
end
end
function obj = setSpeed(obj,set_speed)
obj.speed=set_speed;
end
function showSpeed(obj)
disp(obj.speed)
end
end
end
  4 个评论
Michael Simonovski
Michael Simonovski 2018-6-30
编辑:Michael Simonovski 2018-6-30
The code of the car should be with private properties and public methods:
classdef car
properties (Access = private)
speed
end
methods (Access = public)
function obj = car(set_speed)
if nargin>0
obj.speed=set_speed;
else
obj.speed=0;
end
end
function obj = setSpeed(obj,set_speed)
obj.speed=set_speed;
end
function showSpeed(obj)
disp(obj.speed)
end
end
end
%%Another m.File:
function [output]=func(input)
%How to call the constructor and other methods to
change the private properties?
%How to setting a local variable in this function to the speed value?
For example value=showSpeed(obj);
end
Rik
Rik 2018-6-30
You don't need to explicitly call the constructor. If you want to change the private properties, just change them inside a method. The showSpeed method doesn't return a value. Just use the getter I describe in the comment above. Then you can simply use the syntax I describe.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2018-6-29
i have a class with private properties and public methods to change the properties.
I want to call the methods of the class from a function
Up to this point I follow you. That's easy.
and change the methods.
I don't understand what you're asking here. I suspect you want to change the value of the property without having to return the modified object from the method. If so, you'd want your object to be a handle class. See this documentation page for a discussion of the difference between handle and value classes.
At the end a function display should return a value. Something like this:
value=dispspeed(classcar);
value=10;
If what I guessed about you wanting to use a handle class is not correct, can you explain in a bit more detail exactly how you want instances of your car class to behave?
  2 个评论
Michael Simonovski
Michael Simonovski 2018-6-29
编辑:Michael Simonovski 2018-6-29
1. Call a constructor
function obj = classnew(valuea, valueb)
obj.a=valuea;
obj.b=valueb;
end
%The value should not be returned at this point!
function Output(obj)
?? %At this point i do not know how the body of the function should look like!
end
The display function should output one or two of the properties. I know that in c++ it could be done with return a;
But how it could be done in Matlab?
Steven Lord
Steven Lord 2018-6-30
The constructor for a class and methods of that class are each functions, and can be called just like any other function (with a few exceptions.) The main exception is that to call a method, at least one of the inputs must be an instance of the class for which the method is defined. [I'm ignoring Static methods for purposes of this discussion.]
The easiest way to access properties of an object inside a function is to use dot notation. Basically, inside a function that is not inside the class you can only access public properties. Inside a class method (which as I said above is a function) you can access private properties as well.
function displayObject(obj)
fprintf('Property a of the object: %d\n', obj.a);
end
If you want to return the property, just assign the property to a variable.
function y = returnPropertyA(obj)
y = obj.a;
end
Note that you need to refer to the variable obj. This is particularly important if a method accepts two or more instances of the class, like:
function y = plus(obj1, obj2)
y = obj1.a + obj2.a;
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by