When you instantiate a subclass the superclass constructor will be also executed?
When executing a superclass constructor, my code executes a subclass constructor. To avoid an infinite loop, I don't want the subclass constructor to execute the superclass constructor in this case. How do I avoid this? In particular, running the player class constructor prompts the user for selecting a file which determines which subclass to load the file as: Pfighter or Pwizard. The subclass, Pfighter, is a subclass of the player and character superclasses. This causes the player constructor to execute which causes an infinite loop due to the fact that the subclass can't pass information about the previous iteration to the superclass during instantiation. Again, not desirable. Can I retype the object to the lower class without rerunning the superclass constructor? Show classdef character < handle properties Name Class Level Team Marker HP MP MPcost Speed Gun Range Damage Spellbook card HPmax status ID position positionold legend scatter outofbounds = false levelup = false AIplayer end methods function obj = character(varargin) if nargin == 0 g = game('settings'); n = 1; end obj.status = status(); end function obj = getcard(obj) obj.card = card(obj.AIplayer); fieldnames = fields(obj.card); for j=1:size(fieldnames,1) obj.(fieldnames{j}) = obj.card.(fieldnames{j}); end if obj.AIplayer if strcmp(obj.Class,'Fighter') obj = Cfighter(obj); else obj = Cwizard(obj); end else if strcmp(obj.Class,'Wizard') obj = Pwizard(obj); else obj = Pfighter(obj); end end s = settings; switch obj.Team case 'red' obj.position = [-s.gridsize,-s.gridsize]; case 'green' obj.position = [s.gridsize,s.gridsize]; case 'blue' obj.position = [-s.gridsize,s.gridsize]; case 'yellow' obj.position = [s.gridsize,s.gridsize]; end if obj.AIplayer obj.position = obj.position + [3*rand(),3*rand()]; end obj.positionold = obj.position; end end end classdef player < character properties levels XP end methods function obj = player(obj) obj.AIplayer = false; g = game('settings'); levelsfile = file([g.settings.LevelsPath g.settings.Levelfile],'int'); obj.levels = levelsfile.table; if ~exist(obj.card) obj.getcard; end obj.getXP; end function player = getXP(player) if exist('XP.mat','file') load('XP.mat') else disp('No experience mat file found.'); return end XPexist = false; for j = 1:size(experience,1) if strcmp(experience{j,1},player.Name) player.XP = experience{j,2}; XPexist = true; end if j==size(experience,1) && ~XPexist player.XP = 0; end end end end end classdef Pfighter < player & fighter properties end methods function obj = Pfighter(obj) obj.Class = 'Pfighter'; end end end How does a subclass execute its superclass constructor?To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.
Is super class instantiated when subclass is instantiated?When a subclass is instantiated, instantiate all the superclasses are instantiated at the same time. Here, the initialization of superclass attributes is ensured by the call of the superclass constructors, as described in Inheritance and Constructors.
Can superclass constructors be invoked from subclasses?Invocation of a superclass constructor must be the first line in the subclass constructor.
When you create an object of a subclass the constructor of the superclass calls the constructor of the subclass True or false?(*2) A subclass constructor always calls the constructor of its superclass, and so on up to the Object constructor, to initialize all the aspects of the instance that the subclass inherits from its superclass.
|