Java - defining unknown number of variables in an entity -
i need entity called dynamicentity example, in must define unknown number of variables theyr setters , getters. variables must have name want give them, got:
the main class:
public class main { public static void main(string[] args) { dynamicentity dynamic = new dynamicentity(); dynamic.parsevariable("int"); } }
and dynamicentity class:
public class dynamicentity { public void parsevariable(string text) { string[] tokens = text.split("-"); string variabletype = tokens[0]; string variablename = tokens[1]; if (variabletype.equals("int")) { int variablename = 0; } } }
definetly variablename not accepted due defined already.
the thing dont want variable within if called variablename, want variable called whats within tokens[1].
and of course of study creating setters , getters every new variable added, have no clue this.
you can benefit map structure. key set name of fellow member , value value it.
a simple illustration of concept:
class dynamic { private final map<string,object> members = new hashmap<>(); public void setmember(string name, object value) { members.put(name,value); } public object getmember(string name) { homecoming members.get(name); } }
java
No comments:
Post a Comment