"ismethod" on restricted methods

3 ビュー (過去 30 日間)
Torsten
Torsten 2023 年 1 月 3 日
編集済み: per isakson 2023 年 1 月 6 日
i have class A with a method which is restricted to only be accessible by class B. When i am in class B i want to make sure that class A has this method. "ismethod" however does not work since the method is restricted. I think there should be some way to do this, since i am able to run the method from within B. I could do this with try & catch, but there must be a nicer solution to this.
% class A
classdef classA
methods (Static, Access = ?classB)
function method1
disp("hello, this is method1")
end
end
end
% class B
classdef classB
methods (Static)
function flag = CheckForMethod1(anotherClass)
flag = ismethod(anotherClass, 'method1'); % = false
anotherClass.method1; % this works
end
end
end
% Main
A = classA;
B = classB;
B.CheckForMethod1(A) % should return true, but returns false

回答 (1 件)

per isakson
per isakson 2023 年 1 月 6 日
編集済み: per isakson 2023 年 1 月 6 日
This works
function flag = CheckForMethod1(anotherClass)
mc = metaclass( anotherClass );
flag = ismember( 'method1', {mc.MethodList.Name} );
anotherClass.method1; % this works
end
The function, ismethod, is restricted to public methods by design - I guess.

カテゴリ

Help Center および File ExchangeClass Introspection and Metadata についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by