MATLAB OOP: How to call a method for an object of class A from within a method in an object of class B?

TONGUE:

I am writing object oriented code in MATLAB. I wrote almost all of this, and now, trying to test it, I am faced with what looks like a very fundamental problem.

CODE BACKGROUND:

I have a Window class and a Tracker class. Both are subclasses of the Singleton class (i.e., they have private constructors that provide only one instance of the Window class and class Tracker).

I create an instance of each of them, so now I have an object myWindow and myTracker.

In my main script, I call the myWindow.isNewbead () method. isNewbead is a public method of the Window class.

This is the scene. Now the problem is:

PROBLEM:

Inside isNewbead (), I call myTracker.getpredictedPositions (). getpredictedPositions () is a public method of the Tracker class. However, when I run this line, I get the error "myTracker" undefined. And, of course, I look at the workspace for variables, and the only variables are INSIDE local variables myWindow.isNewbead ();

So, I now have two questions:

QUESTIONS:

  • Is this true for OOP? That is, you cannot call a public method for an object from inside the method in another object without explicitly passing the first object to the method of the second object? It seems to me that I have a lot of trouble, because I use the properties and methods of many objects of different classes in each method, so I have to transfer hundreds of objects every time!

  • If this is only a MATLAB-related issue (for example, a non-static variable problem), then how can I do this?

Thank you very much!

Sincerely.

+4
source share
2 answers

Yes, you can call the public method of one object (object A) from the method of another object (object B), because it is, well, public; however, you need to have a reference to object A for use in the method of object B. Pass the reference to object A as an input argument to the method of object B.

This may seem like a nuisance, but how else can it work? Although the Tracker inherits from the Singleton class, myTracker is still just an instance of the Tracker object. You need a reference to this instance in order to use its methods.

0
source

For Singleton, the template requires a "mechanism to access an element of the singleton class without creating an object of the class." If you pass an instance of a class, you did it wrong. Here is a Matlab implementation using static classes and a global application space. There is another implementation that has an abstract parent from File Exchange , but it will be removed by clear classes . Choose poison.

 classdef MySingleton < handle % %SingletonParent - A class to limit the instances created to one. % %There is a problem with matlab: % clear classes will clear just about any instance, even those stored in % persistent variables inside of functions. This would close any opened singletons % To work around this, we have a method that creates an instance and assigns % it to the appdata structure. This instance can be explicitly killed, but % clear all and clear classes will not kill it. If you ever clear classes, % you will get several messages of this flavor: % % Warning: Objects of 'MySingleton' class exist. Cannot clear this % class or any of its super-classes. % % because of the way we assign and store the singleton, you cannot make % this an abstract parent % % Also, any intialization must be done after you get the instance, since you % have to be able to create it without any data. % properties (Constant) APP_DATA_NAME = 'MySingleton'; end %properties methods (Access = private) function obj = MySingleton() %initialization code. %must be private to ensure getInstance call is the only link into it. end %Singleton end%private methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% methods (Static) function out = getInstance() %getInstance - get/creates Singleton % % stores the instnace such that it is immune to clear all/clear classes % %out = getInstance %Returns % singleton instance. if it does not exist, creates a default one, or passes the data to the ctor if ~isappdata(0, MySingleton.APP_DATA_NAME); obj = MySingleton; setappdata(0, MySingleton.APP_DATA_NAME, obj); end out = getappdata(0, MySingleton.APP_DATA_NAME); end %getMasterInstance() end %static methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% methods %public methods that it uses to work. end %PublicMethods end %MySingleton Class 
+3
source

Source: https://habr.com/ru/post/1391704/


All Articles