Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MATLAB中,是否可以将某个类的对象作为另一个类的类属性?_Matlab - Fatal编程技术网

在MATLAB中,是否可以将某个类的对象作为另一个类的类属性?

在MATLAB中,是否可以将某个类的对象作为另一个类的类属性?,matlab,Matlab,我正在尝试在Matlab2014中制作一个简单的agent和environment类 我正在尝试将代理类的对象“a”作为环境类的属性之一。我已在环境类构造函数中初始化了该对象,但每当我尝试使用该对象访问代理类的方法时,都会收到如下警告: “混淆函数调用。您的意思是引用属性'a'吗?” 这是我的环境类和代理类。如何像JAVA中调用的那样,直接从调用Agent类的方法,并与\u Agent\u函数交互 classdef Environment < handle properties (

我正在尝试在Matlab2014中制作一个简单的agent和environment类

我正在尝试将代理类的对象“a”作为环境类的属性之一。我已在环境类构造函数中初始化了该对象,但每当我尝试使用该对象访问代理类的方法时,都会收到如下警告:

“混淆函数调用。您的意思是引用属性'a'吗?”

这是我的环境类和代理类。如何像JAVA中调用的那样,直接从
调用Agent类的方法,并与\u Agent\u函数
交互

classdef Environment < handle
    properties (Constant = true)
        V = 0.5;
        T = 1;
    end

    properties (SetObservable= true)
        A;
        B;
        a;
    end

    methods
        function obj = initialize(obj, A, B)
            obj.A = A;
            obj.B = B;
            a = Agent();
        end

        function act = call_agent(obj)
            act = agent_function(a, obj.A, obj.B, obj.V, obj.T); 
        end

        function action = interact_with_agent(obj)
            action = obj.call_agent();
        end
    end
end

classdef Agent < handle
    properties (SetObservable = true)
        action;
    end
    methods      
        function action = agent_function(obj, A, B, v, t)
            obj.action = A + v * t * ((B - A) / norm(B - A));
            action = obj.action;
        end
    end
end
classdef环境
这里的问题如下:

  • 在类定义中,您创建了环境类的一个变量(属性):小写
    a

  • initialize
    方法中,创建一个局部变量
    a
    ,该变量在函数完成后被删除。应该使用
    obj.a=…
    将代理()保存在对象中

  • call\u agent
    中,使用未初始化的局部变量
    a
    作为第一个输入。如果您想引用类的
    a
    属性;改用
    obj.a


最重要的是,知道matlab有一个默认的类初始化函数可能会很有用,它与类的名称相匹配;在您的情况下,这将是
函数obj=Agent(obj)
函数obj=Environment(obj)
;有关matlab中类的更多信息,请参见。

您应该这样定义类:

classdef Environment < handle
    properties (Constant = true)
        V = 0.5;
        T = 1;
    end

    properties (SetObservable= true)
        A;
        B;
        a;
    end

    methods
        % Replace here the init function using the Matlab Constructor 
        function obj = Environment(obj, A, B)
            obj.A = A;
            obj.B = B;
            obj.a = Agent(); % Call the Agent constructor here
        end

        function act = call_agent(obj)
            % Calling agent_function will automatically put a as the first argument
            act = obj.a.agent_function(obj.A, obj.B, obj.V, obj.T); 
        end

        function action = interact_with_agent(obj)
            action = obj.call_agent();
        end
    end
end

classdef Agent < handle
    properties (SetObservable = true)
        action;
    end

    methods      
        % Create Constructor
        function obj = Agent()
            obj.action = [];
        end

        function action = agent_function(obj, A, B, v, t)
            obj.action = A + v * t * ((B - A) / norm(B - A));
            action = obj.action;
        end
    end
end
classdef环境
我不知道为什么要使用两个函数来使用属性
a
。 可以在函数
与代理交互
中直接调用函数
agent\u函数


无论如何,如果你真的想用这种方式编码,你应该使用:
methods(static,Access=private)
将函数
call\u agent
设置为static。这样,访问属性
a
的唯一方法是使用函数
interactive\u with_agent

如果要引用成员属性,必须显式使用
obj.a=agent()