【问题标题】:How to get list of children objects from a list of parent objects in java如何从java中的父对象列表中获取子对象列表
【发布时间】:2020-09-11 14:28:27
【问题描述】:

我有一个父子类定义如下:

 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 传递的值?
  • @LuiggiMendoza 我想将 p 作为孩子,但它也不起作用
  • 您这样做的具体目的是什么?除非 p 是 Child,否则您不能将 p 转换为 Child。
  • 我要做的是从我作为输入接收的父对象列表中构建子对象列表。我想要做的是将每个父级的属性保留在父级列表中,但为每个子级添加新属性
  • 你可能需要在Child类中实现一个拷贝构造函数:Child(Parent p) { this.setValue(p.getValue());}

标签: java inheritance arraylist derived-class


【解决方案1】:

试试这个代码:

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_); 的答案中的我的 cmets
【解决方案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);
}

【讨论】:

    【解决方案3】:

    这样的事情怎么样。我添加了类的构造函数。

    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]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2019-08-24
      • 2019-02-06
      相关资源
      最近更新 更多