A constructor call to superclass appears after a return

조회 수: 1 (최근 30일)
Mark Duarte
Mark Duarte 2018년 4월 16일
댓글: Mark Duarte 2018년 4월 16일
I am trying to check if a parameter is of a certain type when instantiating a subclass of a superclass, and I keep getting an error that appears to be associated with my method of checking of the argument type. The debugger won't even let me include a breakpoint without throwing the error "A constructor call to superclass appears after the object is used, or after a return." I think it's pretty obvious what line of code breaks my classes, but why am I not allowed to do this type checking? What other ways are there to confirm that my arguments are of a particular type? Code below.
classdef superClass < handle
properties
PropertyOne
PropertyTwo
end
methods
function sup = superClass(param1, param2)
sup.PropertyOne = param1;
sup.PropertyTwo = param2;
end
end
end
classdef subClass < superClass
properties
PropertyThree
end
methods
function sub = subClass(param1, param2, param3)
if ~isa(param1, 'char')
disp('param1 must be type char')
return
end
sub@superClass(param1, param2);
sub.PropertyThree = param3;
end
end
end

채택된 답변

Philip Borghesani
Philip Borghesani 2018년 4월 16일

Call error instead of disp and return. The constructor must return a valid object in sub when return is called and your code has not initialized it yet.

  댓글 수: 1
Mark Duarte
Mark Duarte 2018년 4월 16일
Thanks Phillip this worked. I will say though that if the disp and return were in the superclass, I am able to use the return statement in the constructor and it escapes without any error.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subclass Definition에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by