date 2 gerarchie di classi:
Ant extends Creature, Troll Extends Creature
e
AntPotion extends Potion,TrollPotion extends Potion
rappresentare il fatto che diversi tipi di pozioni hanno diversi eetti sui diversi tipi
di creature
e realizzando l'implementazione in modo che il metodo si comporti diversamente a secondacodice:class Potion { public void spray( Creature aCreature ) { . . . } . . . }
del tipo di pozione e della creatura.
Completate l'implementazione della classe Potion e di tutte le classi della gerarchia in modo
da ottenere l'effetto desiderato, senza utilizzare il predicato instanceof (nessun testcase sui tipi di oggetto)
esempio:
produce l'output:codice:Potion container = null; Creature aCreature = null; // test Potion on different creatures container = new Potion(); aCreature = new Creature(); container.spray( aCreature ); aCreature = new Troll(); container.spray( aCreature ); aCreature = new Ant(); container.spray( aCreature ); // test TrollPotion on different creatures container = new TrollPotion(); aCreature = new Creature(); container.spray( aCreature ); aCreature = new Troll(); container.spray( aCreature ); aCreature = new Ant(); container.spray( aCreature ); // test AntPotion on different creatures container = new AntPotion(); aCreature = new Creature(); container.spray( aCreature ); aCreature = new Troll(); container.spray( aCreature ); aCreature = new Ant(); container.spray( aCreature );
Potion sprayed on Creature
Potion sprayed on Troll
Potion sprayed on Creature
TrollPotion sprayed on Creature
TrollPotion sprayed on Troll
TrollPotion sprayed on Creature
AntPotion sprayed on Creature
AntPotion sprayed on Troll
AntPotion sprayed on Creature
ho definito
ma l'output dato è:codice:class Ant extends Creature {} class Troll extends Creature {} class AntPotion extends Potion{ public void spray(Creature c){ //print AntPotion on Creature } public void spray(Ant a){ //print AntPotion on Ant } } class TrollPotion extends Potion{ public void spray(Creature c){ //print TrollPotion on Creature } public void spray(Troll a){ //print TrollPotion on Troll } }
POZIONE on CREATURA
POZIONE on CREATURA
POZIONE on CREATURA
TROLL POTION on CREATURA
TROLL POTION on CREATURA
TROLL POTION on CREATURA
ANT POTION on CREATURA
ANT POTION on CREATURA
ANT POTION on CREATURA
sembra che il tipo dinamico di Potion sia rilevato e quindi richiamato l'opportuno metodo della sotto classe,ma il Best Match di sparay con parametro Creature,Troll,Ant è comunque quello con Creature.
Qualcuno è in grado di aiutarmi?Grazie
![]()

Grazie
