java 如何在Java中将父项转换为子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47611401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:42:19  来源:igfitidea点击:

How to cast parent into child in Java

javaclassoopinheritanceparent

提问by likaa

I am doing this:

我正在这样做:

Child child = (Child)parent;

Which gives me an error, I found it isn't possible to do it like this. I don't know exactly why, but I think it should be possible, if Childclass inherits from Parentclass, it contains the Parentobject data.

这给了我一个错误,我发现不可能这样做。我不知道确切的原因,但我认为应该是可能的,如果Child类继承自Parent类,它包含Parent对象数据。

My questions are:

我的问题是:

  • Why it doesnt work?
  • 为什么它不起作用?



  • How can i make this work, without setting every single parent's attribute like this
  • 我怎样才能做到这一点,而不像这样设置每个父母的属性

:

class Parent{
    public int parameter1;//...
    public int parameter1000;
}

class Child extends Parent
{
    public Child(Parent parent)
{
        this.parameter1 = parent.parameter1;//...
        this.parameter1000 = parent.parameter1000;
}
}

回答by Akash Roy Choudhury

Well you could just do :

那么你可以这样做:

Parent p = new Child();
// do whatever
Child c = (Child)p;

Or if you have to start with a pure Parent object you could consider having a constructor in your parent class and calling :

或者,如果您必须从纯 Parent 对象开始,您可以考虑在您的父类中有一个构造函数并调用:

class Child{
    public Child(Parent p){
        super(p);
    }
}
class Parent{
    public Parent(Args...){
        //set params
    }
}

Or the composition model :

或者组合模型:

class Child {
    Parent p;
    int param1;
    int param2;
}

You can directly set the parent in that case.

在这种情况下,您可以直接设置父级。

You can also use Apache Commons BeanUtils to do this. Using its BeanUtils class you have access to a lot of utility methods for populating JavaBeans properties via reflection.

您还可以使用 Apache Commons BeanUtils 来执行此操作。使用它的 BeanUtils 类,您可以访问许多用于通过反射填充 JavaBeans 属性的实用方法。

To copy all the common/inherited properties from a parent object to a child class object you can use its static copyProperties() method as:

要将所有公共/继承属性从父对象复制到子类对象,您可以使用其静态 copyProperties() 方法:

BeanUtils.copyProperties(parentObj,childObject);

Note however that this is a heavy operation.

但是请注意,这是一项繁重的操作。

回答by Stephan-MDD

change 'this' to 'super' in your child constructor, and remove the parent parameter, instead replace that with the two parameters int parameter1;//...int parameter1000; :)

在子构造函数中将“this”更改为“super”,并删除父参数,而是将其替换为两个参数 int parameter1;//...int parameter1000; :)

class Parent{
public int parameter1;//...
public int parameter1000;
}

class Child extends Parent
{
    public Child(int parameter1, int parameter1000)
    {
        super.parameter1 = parameter1
        super.parameter1000 = parameter1000;
    }
}

回答by Edye Chan

You can use json util to do this, such as fastjson.

您可以使用 json util 来执行此操作,例如 fastjson。

Child child = JSON.parseObject(JSON.toJSONString(parent), Child.class);