如何在 Java 中将 parent 转换为 child
How to cast parent into child in Java
我正在这样做:
Child child = (Child)parent;
这给了我一个错误,我发现不可能这样做。我不知道为什么,但我认为应该是可能的,如果 Child
class 继承自 Parent
class,它包含 Parent
object数据。
我的问题是:
- 为什么不起作用?
- 如何在不设置每个 parent 的情况下完成这项工作
像这样的属性
:
class Parent{
public int parameter1;//...
public int parameter1000;
}
class Child extends Parent
{
public Child(Parent parent)
{
this.parameter1 = parent.parameter1;//...
this.parameter1000 = parent.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;
}
}
好吧,你可以这样做:
Parent p = new Child();
// do whatever
Child c = (Child)p;
或者如果你必须从一个纯父对象开始,你可以考虑在你的父对象中有一个构造函数 class 并调用 :
class Child{
public Child(Parent p){
super(p);
}
}
class Parent{
public Parent(Args...){
//set params
}
}
或构图模型:
class Child {
Parent p;
int param1;
int param2;
}
在这种情况下,您可以直接设置父项。
您也可以使用 Apache Commons BeanUtils 来执行此操作。使用它的 BeanUtils class,您可以访问许多实用方法来通过反射填充 JavaBeans 属性。
要将所有 common/inherited 属性从父对象复制到子 class 对象,您可以使用它的静态 copyProperties() 方法:
BeanUtils.copyProperties(parentObj,childObject);
但请注意,这是一项繁重的操作。
你真的想把你 parent 变成 child class 吗?如果给定的 Object(以 Parent object 的形式给出)被创建为 child class,您无需转换即可访问正确的函数。这是一个例子:
在主体中,您看到一个 Vector 'ps' 来存储 Parent object,但添加的是 Child object 从 Parent 扩展而来。在遍历该向量时,我不知道接下来会得到哪种 child(因此我无法转换为正确的 class),但由于关键字 Override 调用的函数,使用了正确的 child/method。
import java.util.Vector;
import lib.Parent;
import lib.C1;
import lib.C2;
public class MyClass {
public static void main(String []args){
System.out.println("Hello World");
C1 c1 = new C1();
C2 c2 = new C2();
ps.add(c1);
ps.add(c2);
for(int k = 0; k < ps.size(); k++){
ps.get(k).setInteger(); // no cast needed
System.out.println("stored entity " + ps.get(k).i);
}
// or use a ranged for loop
for(Parent p: ps){
p.setInteger();
System.out.println("stored entity " + p.i);
}
}
static Vector<Parent> ps = new Vector<>();
}
Parent class:关键字 abstract 强制每个 child 实现该方法,所以如果你有一个 Parent object 你可以安全地调用这个方法。由于 Parent class 本身是抽象的,因此您不会有一个 object 而它只是一个 Parent object 因为那是不允许的并且不会编译。
package lib;
public abstract class Parent{
public Integer i;
public abstract void setInteger();
}
这里有两个 Child class 具有不同的 setInteger() 方法实现。
package lib;
import lib.Parent;
public class C1 extends Parent{
@Override
public void setInteger(){
i = new Integer(1);
}
}
第二个Child:
package lib;
import lib.Parent;
public class C2 extends Parent{
@Override
public void setInteger(){
i = new Integer(2);
}
}
输出是
stored entity 1
stored entity 2
stored entity 1 <-- ranged for
stored entity 2 <-- ranged for
编辑:
文件结构如下所示
|-MyClass.java
|-lib
|-Parent.java
|-C1.java
|-C2.java
我正在这样做:
Child child = (Child)parent;
这给了我一个错误,我发现不可能这样做。我不知道为什么,但我认为应该是可能的,如果 Child
class 继承自 Parent
class,它包含 Parent
object数据。
我的问题是:
- 为什么不起作用?
- 如何在不设置每个 parent 的情况下完成这项工作 像这样的属性
:
class Parent{
public int parameter1;//...
public int parameter1000;
}
class Child extends Parent
{
public Child(Parent parent)
{
this.parameter1 = parent.parameter1;//...
this.parameter1000 = parent.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;
}
}
好吧,你可以这样做:
Parent p = new Child();
// do whatever
Child c = (Child)p;
或者如果你必须从一个纯父对象开始,你可以考虑在你的父对象中有一个构造函数 class 并调用 :
class Child{
public Child(Parent p){
super(p);
}
}
class Parent{
public Parent(Args...){
//set params
}
}
或构图模型:
class Child {
Parent p;
int param1;
int param2;
}
在这种情况下,您可以直接设置父项。
您也可以使用 Apache Commons BeanUtils 来执行此操作。使用它的 BeanUtils class,您可以访问许多实用方法来通过反射填充 JavaBeans 属性。
要将所有 common/inherited 属性从父对象复制到子 class 对象,您可以使用它的静态 copyProperties() 方法:
BeanUtils.copyProperties(parentObj,childObject);
但请注意,这是一项繁重的操作。
你真的想把你 parent 变成 child class 吗?如果给定的 Object(以 Parent object 的形式给出)被创建为 child class,您无需转换即可访问正确的函数。这是一个例子: 在主体中,您看到一个 Vector 'ps' 来存储 Parent object,但添加的是 Child object 从 Parent 扩展而来。在遍历该向量时,我不知道接下来会得到哪种 child(因此我无法转换为正确的 class),但由于关键字 Override 调用的函数,使用了正确的 child/method。
import java.util.Vector;
import lib.Parent;
import lib.C1;
import lib.C2;
public class MyClass {
public static void main(String []args){
System.out.println("Hello World");
C1 c1 = new C1();
C2 c2 = new C2();
ps.add(c1);
ps.add(c2);
for(int k = 0; k < ps.size(); k++){
ps.get(k).setInteger(); // no cast needed
System.out.println("stored entity " + ps.get(k).i);
}
// or use a ranged for loop
for(Parent p: ps){
p.setInteger();
System.out.println("stored entity " + p.i);
}
}
static Vector<Parent> ps = new Vector<>();
}
Parent class:关键字 abstract 强制每个 child 实现该方法,所以如果你有一个 Parent object 你可以安全地调用这个方法。由于 Parent class 本身是抽象的,因此您不会有一个 object 而它只是一个 Parent object 因为那是不允许的并且不会编译。
package lib;
public abstract class Parent{
public Integer i;
public abstract void setInteger();
}
这里有两个 Child class 具有不同的 setInteger() 方法实现。
package lib;
import lib.Parent;
public class C1 extends Parent{
@Override
public void setInteger(){
i = new Integer(1);
}
}
第二个Child:
package lib;
import lib.Parent;
public class C2 extends Parent{
@Override
public void setInteger(){
i = new Integer(2);
}
}
输出是
stored entity 1
stored entity 2
stored entity 1 <-- ranged for
stored entity 2 <-- ranged for
编辑: 文件结构如下所示
|-MyClass.java |-lib |-Parent.java |-C1.java |-C2.java