Class Hierarchy Constructor Syntax - Include all inherited properties;

1 回表示 (過去 30 日間)
CustomBuilt
CustomBuilt 2016 年 9 月 8 日
回答済み: Steven Lord 2016 年 9 月 8 日
I have a class with two parents. I am after a simpler constructor method that encapsulates all the inherited properties, example;
classdef signalEcho < signal & target
properties
deltaT
end
properties (Dependent)
EchoCarrierFrequency
EchoDutyCycle
end
methods
function [thisEcho] = signalEcho(thisSignal, thisTarget, deltaT)
thisEcho = thisEcho@signal(thisSignal.CarrierFrequency, thisSignal.PulseRepFreq, thisSignal.DutyCycle);
thisEcho = thisEcho@target(thisTarget.PositionX, thisTarget.PositionY, thisTarget.Velocity, thisTarget.Heading);
if nargin == 3
thisEcho.deltaT = deltaT;
end
end
Would like to replace the constructor with;
methods
function [thisEcho] = signalEcho(thisSignal, thisTarget, deltaT)
thisEcho = thisEcho@signal(thisSignal);
thisEcho = thisEcho@target(thisTarget);
if nargin == 3
thisEcho.deltaT = deltaT;
end
end
When using the above method no inherited data is populated into the the signalEcho object.

回答 (1 件)

Steven Lord
Steven Lord 2016 年 9 月 8 日
As per the "Initialize Objects Using Multiple Superclasses" section on this documentation page try removing the output argument from your calls to the constructors for the superclasses. So instead of:
thisEcho = thisEcho@signal(thisSignal.CarrierFrequency, thisSignal.PulseRepFreq, thisSignal.DutyCycle);
use:
thisEcho@signal(thisSignal.CarrierFrequency, thisSignal.PulseRepFreq, thisSignal.DutyCycle);

カテゴリ

Help Center および File ExchangeMethods についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by