[英]Reflection: create an instance -java
I'm trying to build a dynamic gui. 我正在尝试构建动态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. 例如:如果他被选为
PropertyDns
我将显示:“ ip1:____ port1:______ ip2:_______ port2:______”当他完成后,他将按“ ok”,然后我要使用该类创建此类的实例。他选择的价值。
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. 看一下
Class.getDeclaredConstructor()
(然后使用您得到的构造函数),或者只是Class.newInstance()
这取决于您的类是否提供构造函数。
See: https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredConstructor-java.lang.Class...- 请参阅: 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. 在不使用反射的情况下 ,通过在
Property
类中定义基本行为,您可以从一组已知的变量中枚举表单元素。
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 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 注意,我在SO编辑不了这一个IDE,它不会编译
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.