【问题标题】:MatLab OOP not changing propertiesMatLab OOP 不改变属性
【发布时间】:2015-07-21 08:25:07
【问题描述】:

我有一个带有“下一个状态”属性的 matlab 类。

我在类中定义了以下函数:

function obj = decideNextAction(obj)
            obj.current_patch_quality
            % Important, rand is redeclared in the two calls. So it may be
            % that rand < current_patch_quality in the first if and greater
            % than in the second if
            if(rand > obj.current_patch_quality)
                obj.next_action = 1;
            elseif(rand < obj.current_patch_quality)
                obj.next_action = 3;
            else
                obj.next_action = 2;
            end   


        end

应该将实例属性重新定义为 1、2 或 3。但是,我做了一些试验,似乎该函数返回了一个带有修改后实例的新对象,但没有修改原始对象。有什么建议吗?

>> x = recruit([0 0])

x =

  recruit with properties:

             nest_location: [0 0]
    current_patch_location: []
     current_patch_quality: 0
               next_action: []

>> x.decideNextAction

ans =

     0


ans =

  recruit with properties:

             nest_location: [0 0]
    current_patch_location: []
     current_patch_quality: 0
               next_action: 1

>> x

x =

  recruit with properties:

             nest_location: [0 0]
    current_patch_location: []
     current_patch_quality: 0
               next_action: []

>>

【问题讨论】:

    标签: matlab oop


    【解决方案1】:

    你必须在classdef中使用handle class

    classdef myclass < handle
    

    如果你使用value class,那么一定是:

    x = x.decideNextAction();
    

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 2021-08-28
      • 2015-07-01
      • 1970-01-01
      • 2013-02-05
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      相关资源
      最近更新 更多