【问题标题】:Understanding MATLAB class properties了解 MATLAB 类属性
【发布时间】:2014-03-30 05:21:38
【问题描述】:

考虑 MATLAB 帮助中的 this example。

这个例子,除了有语法问题外,对我来说并不合适。不知道是不是版本问题,我用的是R2013a。

classdef MyClass
   properties (Constant = true)
      X = pi/180;
   end
   properties
      PropA = sin(X*MyClass.getAngle([1 0],[0 1]);
   end

   methods (Static = true)
      function r = getAngle(vx,vy)

      end
   end
end

上面写着

未定义的函数或变量“X”。 MyClass 中的错误(第 1 行) classdef 我的类

我可以通过添加MyClass.X来修复它,但我不知道这是不是目的。

【问题讨论】:

  • 你应该 report 这是一个文档错误。

标签: matlab matlab-class


【解决方案1】:

That MathWorks example 都搞砸了。可能是这样写的:

classdef MyClass
    properties (Constant = true)
        Deg2Rad = pi/180;
    end
    properties
        PropA = sin(MyClass.Deg2Rad*MyClass.getAngle([1 0],[0 1]));
    end

    methods (Static = true)
        function r = getAngle(vx,vy)
            r = atan2(vy,vx)/MyClass.Deg2Rad;
        end
    end
end

我想重点是演示一个静态方法和常量属性:

>> MyClass.getAngle(1,sqrt(3))
ans =
   60.0000
>> MyClass.getAngle(sqrt(3),1)
ans =
   30.0000
>> MyClass.getAngle(0,1)
ans =
    90

【讨论】:

  • 谢谢。我希望有与 C++ 类似的语法,您不必在前面添加 MyClass。到处都是。
  • 你应该 report 这是一个文档错误。
猜你喜欢
  • 2015-07-27
  • 2019-09-13
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 2019-08-21
相关资源
最近更新 更多