Java
Hi, Suppose I have a class X and two inherited classes Y and Z. I want to have ONE methos, that "knows" which class to construct. I want this method NOT to use a switch-case or if-else statements. For example, in C, a similar probelem, I can set one of the input parameters to be a pointer to a function, and then, when calling with different functions, it will automatically call the input functions. The solution I have right now is using an if-else statement: public void method (String xStr, ...) { X x; if (xStr.equals("Y") x = new Y(...); else if (xStr.equals("Z") x = new Z(...); ... } What do you think ?
Hi, Suppose I have a class X and two inherited classes Y and Z. I want to have ONE methos, that "knows" which class to construct. I want this method NOT to use a switch-case or if-else statements. For example, in C, a similar probelem, I can set one of the input parameters to be a pointer to a function, and then, when calling with different functions, it will automatically call the input functions. The solution I have right now is using an if-else statement: public void method (String xStr, ...) { X x; if (xStr.equals("Y") x = new Y(...); else if (xStr.equals("Z") x = new Z(...); ... } What do you think ?