简体   繁体   中英

Reflection: create an instance -java

I'm trying to build a dynamic gui.

I have the following class:

Public abstract  class Property
String ip1;
int port;

And a lot of sub class created from this class, for example the following two:

public   class PropertyDns extends Property
 string ip2;
 int port2;

Public class PropretyNetBios extends Property
long time;

The goal: The user chooses one of the sub properties and then i want to Present the fields he need to fill in order to create the instance. For example: if he is chosen PropertyDns I Will Present: "ip1:____ port1:______ ip2:_______ port2:______" When he will finish, he will press "ok" and then i want to create an instance of this class with the value he chose.

is there any way it can be done?

i Managed to Present the fields using reflection:

    Field[] s1=p.getClass().getSuperclass().getDeclaredFields();  
            Field[] s=p.getClass().getDeclaredFields();
for (int i = 0; i < s.length; i++) {
    ans[i]=s[i].getName();
}

but i think is Poorly done.

Have a look on Class.getDeclaredConstructor() (then use the constructor you get) or maybe just Class.newInstance() depending if your classes provide constructors or not.

See: https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredConstructor-java.lang.Class...-

Without using reflection and by defining base behavior in your Property class, you can enumerate form elements from a known set of variables.

If you specifically want to use reflection, I suggest you create a basic working example first, and then ask for help if you struggle.

abstract class Property {

    abstract Map<String, String> getPropertyNames();

    abstract void mapProperties(Map<String, String> userInput);

}

class Ip extends Property {

    private String ip;
    private String port;

    public String getIp() {
        return this.ip;
    }

    public int getPort() {
        return Integer.parseInt(this.port);
    }

    Map<String, String> getPropertyNames() {
        Map<String, String> names = new HashMap<>();
        names.put("ip", "IP Address");
        names.put("port", "Port");
    }

    void mapProperties(Map<String, String> userInput) {
        for (Map.Entry<String, String> entry : userInput.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            if ("ip".equals(key)) {
                this.ip = value;
            } else if ("port".equals(key)) {
                this.port = value;
            }
        }
    }
}

GUI

class GUI {

    // psuedo code ahead, warning...

    buildForm() {


        Property p = Property.getInstance(userSelectedType);
        Map m = p.getPropertyNames();
        for (entry in map) {
           // build input type
           // add listener to set values
        }
   }

   void save() {
       // run through user input map
       // call p.mapProperties(userInputMap);
   }
}

note, i type this in SO editor, not an IDE, it will NOT compile

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