How can i call class from a function?
古いコメントを表示
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
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
2018 年 6 月 29 日
編集済み: Michael Simonovski
2018 年 6 月 29 日
採用された回答
その他の回答 (1 件)
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
2018 年 6 月 29 日
編集済み: Michael Simonovski
2018 年 6 月 29 日
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
カテゴリ
ヘルプ センター および File 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!