【问题标题】:changing class properties with its methods使用其方法更改类属性
【发布时间】:2023-04-04 10:24:01
【问题描述】:

我想使用为该类定义的方法更改我的类属性:

classdef triangle<handle

properties
    a
    h         
end

methods

    function obj = triangle()
        obj;
    end

    function obj = setProps(obj, a, h)
        obj.a = a;
        obj.a = h;
    end

end

end

调用:

t = triangle();
t.setProps(a, h);

它根本不起作用 - 我收到此错误:

 The class 'handle' is not a super-class of class 'triangle', as required to invoke a super-class constructor or method.

 Error in triangle (line 13)
    function obj = triangle()

我正在使用 matlab 2012a。我的代码基于这个例子:link

【问题讨论】:

    标签: matlab methods properties


    【解决方案1】:

    在执行此操作之前尝试clear。您可能已经用某些东西覆盖了handle。否则,这适用于 Matlab 2012a:

    clear;
    
    a = 'hello';
    h = 1;
    
    t = triangle();
    t.setProps(a, h);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多