Java vs JavaScript getters and setters methods -


sometimes when have object lots of attributes (for example 30-40) anoying write getter , setter methods in javascript this:

function someobject( properties )  {     // iterate through properties of object, , make sure     // it's scoped.    ( var in properties )     {         (function(){          // create new getter property          this[ "get" + ] = function()          {             return properties[i];          };         // create new setter property          this[ "set" + ] = function(val)          {             properties[i] = val;          };      })(); }  } 

so wondering if possible in java?

suppose if give class 30-40 getters/setters. how feel while using. don't think idea have 30-40 getter/setters in single class. rather break/distribute class subclassing properties

the example gave javascript avery example. on how should access if many properties in single class. in javascript can tread object map.

i apply same idea if have requirement of 30/40 getter setters.

i.e either use java.util.properties

or made custom class

  //only if have 30/40 properties   class myclass {         private map<string, object> data = new concurrenthashmap<string,object>();         public void set(string fieldname, object value) {                data.put(fieldname, value);        }         public object get(string fieldname) {                return data.get(fieldname);        }     } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -