java - Exposure classes -


soo, tried write code when 1 class acts each other class. here class warior "tim" , class witcher "max". console should speak "now max hp.. 50" or "now tim's hp.. 50 hp", console sayed "now max hp.. 25" or "now tim's hp.. 25 hp".. 75 - 25 = 50; 100-50=50; sayed 75 - 25 = 25; 100-50=25; please :c

import java.util.random;  public class maxvstimvoidversion {  public static void main(string[] args) {      warior tim = new warior();     tim.agility = 100;     tim.attack = 25;     tim.hp = 100;     tim.wariorlvl2 = true;      witcher max = new witcher();     max.mana = 150;     max.attack = 50;     max.hp = 75;     max.witcherlvl1 = true;      random = new random();     int b = a.nextint(2);              if (b == 0)             {                 tim.meleeattack();                 system.out.print("now max's hp.." + max.hp);             }              else if (b == 1)             {                 max.magicattack();                 system.out.print("now tim's hp.." + tim.hp);             }     }  }   class people {     static int hp;     static int attack; }  class witcher extends people {     int mana;     boolean witcherlvl1;     boolean witcherlvl2;     void magicattack()     {         warior.hp = warior.hp - witcher.attack;         mana = mana - 20;     } }  class warior extends people {     int agility;     boolean wariorlvl1;     boolean wariorlvl2;     void meleeattack()     {         witcher.hp = witcher.hp - warior.attack;         agility = agility - 20;     } } 

class people {     static int hp = 0;     static int attack = 0; } 

look @ above code. tells hp , attack part of people class , static. so, warrior , witcher same copies [ read shared/same ] people. so, following piece of code

warior tim = new warior(); tim.agility = 100; tim.attack = 25; tim.hp = 100; tim.wariorlvl2 = true;  witcher max = new witcher(); max.mana = 150; max.attack = 50; max.hp = 75; max.witcherlvl1 = true; 

gives over-written effect setting attack 50. so, in meleeattack() or magicattack(), end subtracting 50 instead of appropriate value.

to understand more, try setting max.attack = 0;

also, should passing reference of victim in meleeattack() or magicattack() gives clarity.

void magicattack( warrior warrior ); 

i suggest remove static modifiers people class. should trick it, warrior , witcher end own copies of variables.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -