java - Methods behaving like Class Fields -
sometimes need values made of more 1 value underlying values can change.
example
in role playing game written in java there class represents player. have fields "life" , "endurance". last field increase life of player. have "life" , life increasing attribute "endurance" results in total or full life.
i write that:
public class player { private int life; private int endurance; private int life_total() { return life + endurance; } public player() { life = 10; endurance = 2; } public int get_life_total() { return life_total() } }
i write method looks field brackets , behaves field. acceptable solution or there better solutions such situations?
in languages have function references javascript, acceptable use such function references returning composed values?
your getter method should more this. there no reason have getter method call method fetch variable.
public int get_life_total(){ return life + endurance; }
Comments
Post a Comment