java - Getting around making constructor call the first statement in a constructor -


i java newbie, hence simple question:

i have class constructor takes 1 argument follows:

class mybase {     public mybase(objecttype1 o1) {}     ... } 

i want derive class class constructor takes string , calls constructor of base class proper argument chosen on basis of argument follows:

class myderived extends mybase {      public myderived(string objecttype) {           objecttype o = null;           if (objecttype.equals("type1")               o = a; /* value */           else               o = b; /* other value */             super(o);      } 

the problem code constructor call must first statement in constructor. how solve problem? don't want make decision objecttype outside myderived. prefer avoid having provide static createobject method myderived.

it sounds you're reinventing enums.

class base {     static enum option {         alpha, beta, gamma;     }      public base(option o) {         // ...     } }  class derived extends base {     public derived(string s) {         super(option.valueof(s));     } } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -