【问题标题】:Class handle. Matlab类句柄。 MATLAB
【发布时间】:2015-11-27 18:08:33
【问题描述】:

我有 2 个类似的课程:

classdef class1 < handle

    properties (Access = public)
        b
    end

    properties (Access = private)
        a
    end

    methods (Access = public)
        function this = class1()
            this.a = 1;
            this.b = 1;
        end

        function test(this)
            'Inside class'
            this.a
            this.b
            this.a = 2;
            this.b = 2;
        end
    end

end

第二个:

classdef class2

    properties (Access = public)
        b
    end

    properties (Access = private)
        a
    end

    methods (Access = public)
        function this = class2()
            this.a = 1;
            this.b = 1;
        end

        function test(this)
            'Inside class'
            this.a
            this.b
            this.a = 2;
            this.b = 2;
        end
    end

end

有一次我从句柄继承。另一种我不做。创建这样的脚本后:

solver1 = class1();
solver1.b
solver1
solver1.test()
solver1.b
solver1
solver1.test()



solver2 = class2();
solver2.b
solver2
solver2.test()
solver2.b
solver2
solver2.test()

如果我一步一步调试我的程序,我发现 ab 没有改变在solver2.test() 之后的第二节课。但是第一类这些变量在solver1.test() 之后发生了变化。这个问题的原因是什么?

【问题讨论】:

  • 这是handle class 的一个特性,您从其继承第一个类。
  • 我明白了。什么处理类?
  • 一会儿。我又读了一遍。

标签: matlab matlab-class


【解决方案1】:

我用下一个方法解决了这个问题。我将全局变量声明为类。在此之后,我可以在课堂内外检查任何变量。这是不好的方式,但它的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多