java - Purpose of getters/setters in this class -
this question has answer here:
- why use getters , setters? 38 answers
in class, have constructor take in 4 parameters. , values initialized appropriately. set int imageid mimageid, etc..
what purpose of sequence of getters/setters if member variables initialized parameters?
from i've read. getters/setters every field or member variable makes class "public". or purpose of getters/setters broadcast "intent" class?
public class page { private int mimageid; private string mtext; private choice mchoice1; private choice mchoice2; public page(int imageid, string text, choice choice1, choice choice2 ){ mimageid = imageid; mtext = text; mchoice1 = choice1; mchoice2 = choice2; } // start of getters/setters public int getimageid(){ return mimageid; } public void setimageid(int id){ mimageid = id; } public string gettext() { return mtext; } public void settext(string text) { mtext = text; } public choice getchoice1() { return mchoice1; } public void setchoice1(choice choice1) { mchoice1 = choice1; }// choice 1 public choice getchoice2() { return mchoice2; } public void setchoice2(choice choice2) { mchoice2 = choice2; }// choice 2
a setter lets change value of variable later on.
a getter lets access value of variable class.
in context, suppose page
describes page content. now, suppose pass page
object sort of layout class, use getters access values of variables render layout. variables declared private, there no other way access them, except using getter methods.
Comments
Post a Comment