0

例: 次のような 2 つのクラスがあります: クラス B のコメントの下にある質問を探します。

classdef A < handle
    properties
        classBobj; % class B is a property of class A
    end

    methods
        MethodFromA (obj)
        end
    end
end

classdef B <handle
    methods
        MethodFromB (obj)
            % I is possible to call class A method MethodFromA here
        end
    end
end
4

1 に答える 1

1

これを可能にする方法は 2 つあります。

  1. クラス A から静的メソッドを呼び出そうとしますが、次のように定義する必要があります。

    classdef A < handle
    
        properties
            classBobj; % class B is a property of class A
        end
    
        methods (Static)
            MethodFromA()
        end
    
    end
    

これは、次のようにコード内のどこでも呼び出すことができます (A のインスタンスへの参照なし)。A.MethodFromA()

  1. クラス A のインスタンスへの参照があるため、 を呼び出す代わりに、MethodFromB(obj)すべてMethodFromB(obj, classAobj)
于 2013-03-07T08:43:12.467 に答える