简体   繁体   中英

Polymorphism or change name of variable java

I wanna change a variable's name by choice of user. I know that it can be done by Mab (not very charming), but I think that it can be done by polymorphism too, at least something that can simulate it. It is hard to explain, so the code below can illustrate it better. Thanks!

        public class Main {
            public static void main(String[] args) {
                GenericObject o;

                o = new Object1(10, 10);
                o.wh();
                System.out.println(o.w); // Output: 3 (ok)
                System.out.println(o.h); // Output: 10 (ok)

                o = new Object2(10, 10);
                o.wh();
                System.out.println(o.w); // Output: 7 (ok)
                System.out.println(o.h); // Output: 4 (ok)

                String inputFromUser = "1";
                o = new Object + inputFromUser + (10, 10); /*I know that is an absurd, just to illustrate...
if polymorphism can solve this problem, I thik it's the best option. So how use it here?
I don't wanna use ifs or switchs, I will use more than 300 classes*/
                o.wh();
                System.out.println(o.w); // Output: 3 (that's what I wanna obtain)
                System.out.println(o.h); // Output: 10 (that's what I wanna obtain)
            }
        }
        abstract class GenericObject {
            int w, h, x, y;
            GenericObject (int x, int y) {
                this.x = x;
                this.y = y;
            }
            public abstract void wh();
        }
        class Object1 extends GenericObject{
            Object1 (int x, int y) {
                super(x, y);
            }
            @Override
            public void wh () {
                w = 3;
                h = 10;
            }
        }
        class Object2 extends GenericObject{
            Object2 (int x, int y) {
                super(x, y);
            }
            @Override
            public void wh () {
                w = 7;
                h = 4;
            }
        }

Hope below code will help you.

            HashMap<String, GenericObject > allTypes = new HashMap<>();
            allTypes.put("1", new Object1(10, 10));
            allTypes.put("2", new Object2(10, 10));

            String inputFromUser = "1";
            GenericObject o = allTypes.get(inputFromUser);
            o.wh();
            System.out.println(o.w); // Output: 3 
            System.out.println(o.h); // Output: 10

All design smells aside, I think that you're really looking for an answer using reflection. I think you can use the following code snippet to accomplish what you want:

    // dynamically specify the class name based on the user's input.
    Class<?> typeFromUser = Class.forName("Object" + inputFromUser);
    // grab the correct package-private or public constructor whose type
    // parameters are 2 primitive ints
    java.lang.reflect.Constructor<?> constructor =
        typeFromUser.getDeclaredConstructor(Integer.TYPE, Integer.TYPE);
    // reflectively invoke the constructor with the two int values
    Object obj = constructor.newInstance(10, 10);
    // the instantiated object is of a raw type, cast it to the correct type
    o = (GenericObject) obj;

I also tried this code and you can see the demo on IDEOne .

I think you can use reflection mechanism to do this. Below is the example code snippet:

    String clsName = <your input(Object1 or Object2)>;
    Class<?> clazz = Class.forName(clsName);
    Constructor<?> cst = clazz.getDeclaredConstructor(int.class, int.class);
    GenericObject obj = null;
    if(clsName.equals("Object1")) {
        obj = (Object1)cst.newInstance(10, 10);
    } else if(clsName.equals("Object2")) {
        obj = clsName.equals("Object1")
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM