本文介绍了此MATLAB类适用于什么,为什么它不能在我的PC上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文档中的第一个: http://www.mathworks.com/help/matlab /matlab_oop/getting-familiar-with-classes.html

The very first one in this documentation:http://www.mathworks.com/help/matlab/matlab_oop/getting-familiar-with-classes.html

班级是:

classdef BasicClass
   properties
      Value
   end
   methods
      function r = roundOff(obj)
         r = round([obj.Value],2);
      end
      function r = multiplyBy(obj,n)
         r = [obj.Value] * n;
      end
   end
end

当我这样运行时

a = BasicClass
a.Value = pi/3;

它工作正常并且可以完成这段代码

It works fine and does what it should but this piece of code

a = BasicClass(pi/3);

给出以下错误:

使用回合错误

输入参数过多."

是什么意思? (我正在使用R2014a)在Matlab中使用oop是愚蠢的吗?哈哈

What does it mean? (I'm using R2014a) Is it stupid to use oop in Matlab? LOL

推荐答案

与代码相比,您的错误消息看起来都不正确,无论您缺少类构造函数(在帮助链接的中途提到):

Your error message doesn't look correct compared with the code, whichever your missing a class constructor (as is mentioned at the half way down the help link):

classdef BasicClass
  properties
    Value
  end
  methods

    % Class constructor -> which you can pass pi/3 into.
    function obj = BasicClass ( varargin )
      if nargin == 1
        obj.Value = varargin{1};
      end
    end

    % Your Methods
    function r = roundOff(obj)
      r = round([obj.Value],2);
    end
    function r = multiplyBy(obj,n)
      r = [obj.Value] * n;
    end
  end
end

这篇关于此MATLAB类适用于什么,为什么它不能在我的PC上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 13:53