根据类设置和获取字段

时间:2015-02-15 23:57:51

标签: java netbeans

我有两个包:Presentation,其中主类是一类方法,
以及包含Entities:Entity1和Entity2的包 Entity1有private String entity1var;
Entity2有private Integer entity2var;

类看起来像这样:

public class Entity1 {
private String entity1var;

/**
 * @return the entity1var
 */
public String getEntity1String() {
    return entity1var;
}

/**
 * @param entity1String the entity1var to set
 */
public void setEntity1String(String entity1String) {
    this.entity1var = entity1String;
}
}

并且:

public class Entity2 {
private Integer entity2var;

/**
 * @return the entity2var
 */
public Integer getEntity2Integer() {
    return entity2var;
}

/**
 * @param entity2Field the entity2var to set
 */
public void setEntity2Integer(Integer entity2Field) {
    this.entity2var = entity2Field;
}
}

主要班级:

public class Main {
public static void main(String ARGS[]){
    Methods _methods = new Methods();
    _methods.doSomething("Entity1");
    _methods.doSomething("Entity2");
}
}

方法类:

public class Methods {
public void doSomething(String newClass){
    try {
        Class _class =  Class.forName("Entities."+newClass);
        //How can I set the variable on each class 
        //and get the value after 

        //Something similar to this:
        //_class.setEntity1String("Im new");
        //System.out.println(_class.getEntity1String); -> Result: Im new

        //_class.setEntity2Integer(777);
        //System.out.println(_class.getEntity2Integer); -> Result: 777


    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Methods.class.getName()).log(Level.SEVERE, null,         ex);
    }
}
}

我尝试过这样的事情:

    Class newClass = Class.forName("Entities."+className);
    Method newMethod = newClass.getDeclaredMethod("getEntity1String");
    Object newValue = newMethod.invoke(newClass.newInstance());
    System.out.println(newValue);

如果我给它一个值private String entity1var = "Something new",它确实会显示变量中的值但是如何在不使用经典实例化的情况下更改值?我想避免这种情况,因为我不知道我会收到的课程:

Entity1 entity = new Entity1();
entity.setEntity1String("Im also new");

解决方案(感谢immibis):

        Class _class = Class.forName("Entities." + newClass);
        Object _classInstance = (Object) _class.newInstance();
        Method _newMethod = _class.getDeclaredMethod("setEntity1String", String.class);
        _newMethod.invoke(_classInstance, "New String");

        Method _method = _class.getDeclaredMethod("getEntity1String");
        Object _methodValue = _method.invoke(_classInstance);
        System.out.println(_methodValue);

1 个答案:

答案 0 :(得分:1)

您使用这样的反射调用了getEntity1String

Class newClass = Class.forName("Entities."+className);
Method newMethod = newClass.getDeclaredMethod("getEntity1String");
Object newValue = newMethod.invoke(theInstanceYouWantToCallTheMethodOn);

您可以使用反射调用setEntity1String,方法如下:

Class newClass = Class.forName("Entities."+className);
Method newMethod = newClass.getDeclaredMethod("setEntity1String", String.class); // argument types need to be passed to getDeclaredMethod
newMethod.invoke(theInstanceYouWantToCallTheMethodOn, newValue); // need to pass the actual argument values now; also we don't care about the return value so it's not assigned to anything