Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法将对象转换为现有的pojo_Java_Spring_Methods_Casting_Pojo - Fatal编程技术网

Java 方法将对象转换为现有的pojo

Java 方法将对象转换为现有的pojo,java,spring,methods,casting,pojo,Java,Spring,Methods,Casting,Pojo,所以,我有这样一个pojo: public class pojo { String name; String address; String email; // getter's and setter's here... } public void method(Object obj) { **Dynamically detect obj type** newObj = (obj type) obj; // I want to do like this

所以,我有这样一个pojo:

public class pojo {
   String name;
   String address;
   String email;

   // getter's and setter's here...
}
public void method(Object obj) {

   **Dynamically detect obj type** newObj = (obj type) obj;

   // I want to do like this, because this method will never know what 
   // object type I am passing.
   // I will have more than 10 pojo's and I wanted the method to detect
   // and create them dynamically.
}
对于一个接收类型对象的方法,如何将其转换为原始类型,以便使用所有getter和setter

private void method(Object obj) {
   // use get's and set's from the original object of type "pojo"
   // instead of the methods from java.lang.Object
}

我真的不知道如何更好地解释它,这让我有点困惑。希望你们都能理解。 提前谢谢

编辑1
所以,我没有解释清楚,因为我有点困惑,我甚至不知道我想做的事情是否可能。但是,在这个方法中,我想做一些类似的事情:

public class pojo {
   String name;
   String address;
   String email;

   // getter's and setter's here...
}
public void method(Object obj) {

   **Dynamically detect obj type** newObj = (obj type) obj;

   // I want to do like this, because this method will never know what 
   // object type I am passing.
   // I will have more than 10 pojo's and I wanted the method to detect
   // and create them dynamically.
}
你可以使用铸造

在这里,我们在一个空构造函数中创建一个Pojo对象,然后将其作为对象发送给该方法

public static void main(String[] args) {
        
        Pojo pojo=new Pojo();
        pojo.method(pojo);
        System.out.println(pojo.getName());

    }
我们的方法将obj作为一个通用java对象,并将其作为一个Pojo对象强制转换到已经是Pojo对象的测试中(我们这样做是为了使用Pojo类的方法)。 然后我们设定它是一个名字

public class Pojo {
    
    private String name;
    private String address;
    private String email;
    
    public Pojo(){}
     
     public Pojo(String name,String address,String email)
     {
       this.name=name;
       this.address=address;
       this.email=email;
           
     }
     
     
     
     public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public void method(Object obj) {
        Pojo test = (Pojo)obj;
        test.setName("avi");
     }
}
你必须使用铸造

private void method(Object obj) {
   Pojo p = (Pojo) obj;
  
   //Now you can use the pojo methods.
   p.getName(); //And other methods depending on what methods you have defined in Pojo
}
编辑:或者您可以使用,然后使用适当的铸造

private void method(Object obj) {
   Pojo p = (Pojo) obj;
  
   //Now you can use the pojo methods.
   p.getName(); //And other methods depending on what methods you have defined in Pojo
}
要获取类的名称,请使用以下命令:
Class c=obj.getClass()


然后,您可以使用开关盒来使用适当的强制转换。

在强制转换之前使用
instanceof
进行测试

private void method(Object obj) {
   Pojo p = (Pojo) obj;
  
   //Now you can use the pojo methods.
   p.getName(); //And other methods depending on what methods you have defined in Pojo
}
Pojo Pojo=Pojo的obj实例?(Pojo)Pojo:newpojo()

试试看

Pojo Pojo=new ObjectMapper().readValue(object,Pojo.class)


这会将您的对象转换为pojo类型,因此可以访问pojo中的所有方法。

您只需使用
instanceof
操作符检查即可检测对象的类型

如果您的目的是动态创建对象,请转到反射来创建对象

public void method(Object obj) throws Exception {

    Class<?> clz = obj.getClass();
    Object newObj = clz.newInstance();

    **Dynamically detect obj type** newObj = (obj type) obj;

    // I want to do like this, because this method will never know what 
    // object type I am passing.
    // I will have more than 10 pojo's and I wanted the method to detect
    // and create them dynamically.
}
public void方法(Object obj)引发异常{
类clz=obj.getClass();
对象newObj=clz.newInstance();
**动态检测obj类型**newObj=(obj类型)obj;
//我想这样做,因为这种方法永远不会知道什么
//我正在传递的对象类型。
//我将有10多个pojo,我希望该方法能够检测到
//并动态创建它们。
}

由于方法返回类型为void,您必须在该方法内执行业务逻辑,根据不同的对象类型,您需要应用业务逻辑。然后显式地检查对象类型以在if块中执行任务。

obj是什么样子的?它只是一个java.lang.object@PhilippWilhelm。我试图使这个方法成为动态的,所以他接收的对象的类型无关紧要,他将始终使用pojo中现有的getter和setter,您希望用这种方法实现什么?如果您不知道对象的类型,您将如何调用这些方法?@M.Deinum,因为我有一个创建SOAP信封的方法,我不想为我拥有的每个对象类型创建SOAPBody。因为如果我有100+pojo,我就需要100+复制这个方法。为什么不使用SpringWS发送/创建信封呢?你为什么自己这么做?对不起,但不是我想要的,我编辑了这个问题,这样我可以更好地解释我自己。对不起,但不是我想要的,我编辑了这个问题,这样我可以更好地解释我自己。对不起,但不是我真正想要的,我对问题进行了编辑,以便更好地解释自己。我对答案进行了编辑,以包含有关使用Java反射API获取类名的信息。对不起,这不是我真正想要的,我对问题进行了编辑,以便更好地解释我自己。