필터 지우기
필터 지우기

require subclasses to support the same number of input and output arguments as the abstract method of their super class

조회 수: 2 (최근 30일)
in the documentation about abstract methods in an abstract class it is mentioned that:
  • Concrete subclasses are not required to support the same number of input and output arguments and do not need to use the same argument names. However, subclasses generally use the same signature when implementing their version of the method.
what I want is exactly the opposite, i.e. I want Concrete subclasses are required to support the same number of input and output arguments and otherwise I get an error. How is it possible?

채택된 답변

Matt J
Matt J 2022년 2월 13일
편집: Matt J 2022년 2월 13일
In the base class constructor, you can check the function signature, like in this example:
classdef myclass
methods
function obj=myclass()
ml=getfield(metaclass(obj),'MethodList');
meth=ml({ml.Name}=="myMethod");
assert(isequal(numel(meth.InputNames),4) && ...
isequal(numel(meth.OutputNames),2),...
"myMethod has incorrect signature in subclass "+class(obj))
end
end
methods(Abstract)
[out1,out2]=myMethod(obj, in1, in2, in3)
end
end
  댓글 수: 2
Masa
Masa 2022년 2월 14일
this could be achieved easily, if there was an attribute for example restrictInOuts for methods:
abstract methods with restricted number of inputs and outputs:
methods(Abstract, restrictInOuts=true)
[out1,out2]=myMethod(obj, in1, in2, in3)
end
abstract methods with any arbitrary number of inputs and outputs:
methods(Abstract, restrictInOuts=false)
myMethod(obj)
end
Matt J
Matt J 2022년 2월 14일
You could ask them to implement it, but I doubt it's as easy as that. When the signature contains varargin or varargout, I imagine things get complicated.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by