需要使用方法参数中传递的字段名动态创建对象。
就像..。
public void createDisplay(List<String> fields) {
//fetch fields names
//create an object in java using the fields passed
//assign values to the dynamically created fields.
}或
public void createDisplay(Schema schema) {
//create an object in java using the schema
//assign values to the dynamically created fields.
}有人能帮上忙吗。
发布于 2018-10-16 03:11:40
你的问题有点模糊。如果你真的想从你的方法中创建完全初始化的对象,那么我建议你这样做:
public void createDisplay(List<String> fields) {
//Iterate over the list do null checking wherever required
//call parameterized constructor of your class and pass values in the loop and keep on adding them
//change the return type of method from void to the object/list of object
//you want and return it from this method
}发布于 2018-10-16 05:28:25
除非你完全确定你在做什么,否则在运行时这是不值得的。相反,将这些精力投入到编译时解决方案中可能更有价值。
有一个名为ByteBuddy的库,它具有动态生成类型的能力,但我不相信在这方面有太多的实用程序,因为你想在系统中动态地创建一个合适的类型。您将面临的最大问题是:
我强烈建议您转而研究编译时解决方案。运行时解决方案因其灵活性而颇具吸引力,但Java不遗余力地成为_in_flexible。
https://stackoverflow.com/questions/52823171
复制相似问题