gpt4 book ai didi

oop - MATLAB 面向对象 : How do I call a method on an object of class A from inside a method in an object of class B?

转载 作者:太空宇宙 更新时间:2023-11-03 20:03:01 25 4
gpt4 key购买 nike

语言:

我正在 MATLAB 中编写面向对象的代码。我几乎编写了所有内容,现在在尝试对其进行测试时遇到了一个看起来非常基本的问题。

代码背景:

我有一个 Window 类和一个 Tracker 类。两者都是 Singleton 类的子类(即,它们具有私有(private)构造函数以确保仅创建类 Window 和类 Tracker 的一个实例)。

我实例化了它们中的每一个 - 所以我现在有一个 myWindow 和 myTracker 对象。

在我的主脚本中,我调用了一个方法 myWindow.isNewbead()。 isNewbead 是 Window 类的公共(public)方法。

就是这个场景。现在的问题:

问题:

在 isNewbead() 内部,我调用 myTracker.getpredictedPositions()。 getpredictedPositions() 是类 Tracker 的公共(public)方法。但是,当我运行这一行时,我收到一条错误消息,指出变量“myTracker”未定义。果然,我查看了变量工作区,那里唯一的变量是局部变量 INSIDE myWindow.isNewbead();

所以我现在有两个问题:

问题:

  1. OOP 是否无处不在?也就是说,在不显式地将第一个对象传递给第二个对象的方法的情况下,是否可以从另一个对象的方法内部调用一个对象的公共(public)方法?这对我来说似乎很麻烦,因为我在每个方法中都使用了很多不同类的对象的属性和方法,所以我每次都必须传递数百个对象!

  2. 如果这只是一个特定于 MATLAB 的问题(例如非静态变量的问题),那么我该如何解决它?

非常感谢!

问候。

最佳答案

对于单例,该模式需要“一种无需创建类对象即可访问单例类成员的机制”。如果你传递类的实例,你就做错了。这是一个使用静态类和全局 appdata 空间的 Matlab 实现。有 another implementation确实有一个来自 File Exchange 的抽象父级, 但它将被 clear classes 删除。选择你的毒药。

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

关于oop - MATLAB 面向对象 : How do I call a method on an object of class A from inside a method in an object of class B?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8917234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com