0

我有 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()

如果我一步一步调试我的程序,我看到absolver2.test(). 但是第一堂课以后这些变量都变了solver1.test()。这个问题的原因是什么?

4

1 回答 1

0

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

于 2016-03-29T13:37:13.637 回答