如何从Java中的父对象列表获取子对象列表

3

我有一个父类和子类,如下所示:

 public class Parent {
  public int values_ = 0;

  public void setValue(int v)
  {
    this.values_ = v;
  }
}

并添加以下子类:

public class Child extends Parent {
   public double key = 3;
}

我希望获得一个儿童列表,其中每个孩子在子列表中将具有父对象列表中每个父对象的属性。

我尝试了以下内容:

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub


    ArrayList<Parent> parentList = new ArrayList<Parent>();

    Parent p1 = new Parent();
    p1.setValue(10);
    parentList.add(p1);

    Parent p2 = new Parent();
    p1.setValue(20);
    parentList.add(p2);


    ArrayList<Child> childrenList =  new ArrayList<Child>();
    for(Parent p : parentList)
    {
        Child c =  new Child();
        System.out.println(c.values_);
        System.out.println(c.key);

        childrenList.add(c);

    }

}

}

但是它不起作用。 我的孩子仍然具有父级的默认值,而不是设置的值。
我该如何处理?

在最后一个 for 循环中,您从未使用 Parent p 变量。您如何期望 c 有一个值,而您又没有从 p 传递它呢? - Luiggi Mendoza
@LuiggiMendoza 我想将p强制转换为子类,但也没有起作用。 - user3841581
你这样做的确切目的是什么?除非p是Child,否则你不能将p转换为Child。 - WJS
我试图做的是从一个作为输入的父对象列表中构建子对象列表。我想做的是保留父对象列表中每个父对象的属性,但为每个子对象添加新属性。 - user3841581
Child类中,您可能需要实现一个复制构造函数:Child(Parent p) { this.setValue(p.getValue());} - Nowhere Man
3个回答

2
像这样的东西怎么样。我添加了类的构造函数。
class Parent {
    public int values_ = 0;

    public Parent(int values) {
        this.values_= values;
    }
    public void setValue(int v) {
        this.values_ = v;
    }

}

class Child extends Parent {
    public double key = 3;

    public Child(int values) {
       super(values);
    }
    public Child(double key, int values) {
        super(values);
        this.key = key;
    }
    @Override 
    public String toString() {
        return "[" + key + " " + values_ + "]";
}

ArrayList<Parent> parentList = new ArrayList<Parent>();

Parent p1 = new Parent(10);
parentList.add(p1);

Parent p2 = new Parent(20);
parentList.add(p2);

ArrayList<Child> childrenList = new ArrayList<Child>();
int key = 4;
for (Parent p : parentList) {
     // add values from parent and key from here
    Child c = new Child(key++, p.values_);
    // if wanted to use the default key in the child class,
    // then just use the value constructor
    //  Child c = new Child(p.values_);
    childrenList.add(c);
}

System.out.println(childrenList);


打印

[[4.0 10], [5.0 20]]

2
你在这里做的不对,你只是在遍历父列表并在每次迭代中创建一个新的子对象,但没有为子对象分配任何值,因此它将具有默认值。
尝试这样做:
public class Parent {
  public int values_ = 0;

  public void setValue(int v)
  {
    this.values_ = v;
  }
}

public class Child extends Parent {
   public double key = 3;
}

public static void main(String[] args) {
    List<Parent> parentList = new ArrayList<Parent>();

    Parent p1 = new Parent();
    p1.setValue(10);
    parentList.add(p1);

    Parent p2 = new Parent();
    p2.setValue(20);
    parentList.add(p2);


    List<Child> childrenList =  new ArrayList<Child>();
    for(Parent p : parentList) {
        Child c =  new Child();
        c.values_ = p.values_;
        childrenList.add(c);
    }

    System.out.println(childrenList);
}

2

尝试使用以下代码:

class Parent {
    public int values_ = 0;

    public Parent(int values) {
        this.values_= values; 
    }
}
class Child extends Parent {
    public double key = 3;

    public Child(int values) {
        super(values); 
    }

    @Override 
    public String toString() {
        return "[" + key + " " + values_ + "]";
    }
}
public class TestInherit {

    public static void main(String...string) {
        ArrayList<Parent> parentList = new ArrayList<Parent>();

        Parent p1 = new Parent(10);
        parentList.add(p1);

        Parent p2 = new Parent(20);
        parentList.add(p2);

        ArrayList<Child> childrenList =  new ArrayList<Child>();
        for(Parent p : parentList)
        {
            Child c = new Child(p.values_);
            childrenList.add(c);
        }
        System.out.println(childrenList);
    }

我猜你没有读我的答案,因为你基本上重复了它(请看我的评论关于new Child(p.values_);的答案)。 - WJS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接