gpt4 book ai didi

java - 根据类设置和获取字段

转载 作者:行者123 更新时间:2023-12-01 11:54:44 24 4
gpt4 key购买 nike

我有 2 个包:Presentation,其中主类带有类Methods,
以及一个包含实体的包:Entity1 和 Entity2。
Entity1 有 private StringEntity1var;
Entity2 有私有(private)整数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 StringEntity1var = "Something new" 但如何在不使用经典实例化的情况下更改该值?我想避免这种情况,因为我不知道我将收到什么类(class):

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);

最佳答案

您使用反射调用了 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

关于java - 根据类设置和获取字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28532710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com