将子类对象添加到ArrayList

时间:2014-10-08 14:03:48

标签: java arraylist subclass

我大约一个月进入我的软件学士学位,我们正在研究这个森林项目,我们有不同类型的树木(灰烬和山毛榉),它们以不同的速度生长。它们是通用类Tree的子类。

我的问题是现在我被要求将每种树(灰和山毛榉)的两棵树添加到ArrayList中 - 他们的int agedouble height都应该是不同的。我根本无法理解应如何设置这一点,因此非常感谢任何建议/提示/解决方案。

Tree

的来源
public class Tree{

public int age;
public double height;

public void growOneYear()
{
    age++;
}

public void show()
{
    System.out.println("Træet er " +age+ " år gammelt og " +height+ " meter højt.");
}
}

Ash的来源(几乎与比奇相同)

public class Ash extends Tree {

public Ash()
{
    age = 1;
    height = 1.0;
}

public void growOneYear()
{
    super.growOneYear();
        if(height < 15){
            height = height*1.20;
        }
}

public void show()
{
    System.out.println("Ask: ");
    super.show();
}
}

Screenshot of the structure

3 个答案:

答案 0 :(得分:1)

使用继承。

使用Ash类声明BeechTree对象应该允许您将Tree对象添加到trees集合。

Tree ash = new Ash();
Tree beech = new Beech();
ash.growOneYear();
beech.growOneYear();
trees.add(ash);
trees.add(beech);

答案 1 :(得分:1)

考虑到您已经在类声明中将Tree子类化为extends Tree,此时您需要做的就是存储Tree的所有树和子类在List Tree中的s基本上是创建列表,并将树添加到列表中。

public class Forest
{
    private List<Tree> trees;

    public Forest()
    {
        trees = new ArrayList<>(); //diamond syntax Java 7, ArrayList<Tree>() on Java 6
    }

    public void addTree(Tree tree)
    {
        trees.add(tree);
    }

    public void growTreesByOneYear()
    {
        for(Tree tree : trees)
        {
            tree.growOneYear();
        }

        //you can do trees.stream().forEach(x -> x.growOneYear()); on Java 8
    }

    public void showTrees()
    {
        for(Tree tree : trees)
        {
            tree.show();
        }
    }
}

public class MainClass
{
    public static void main(String[] args)
    {
        Forest forest = new Forest();
        Ash ashTree = new Ash();
        Beech beechTree = new Beech();
        forest.addTree(ashTree);
        forest.addTree(beechTree);
        forest.show();
        forest.growTreesByOneYear();
        forest.show();
    }
}

我希望有所帮助!

哦,对于树 - 你可以在构造函数参数中提供年龄和高度,并创建它们:

Ash ash = new Ash(5, 1.5); //age, height

哪个会用

public class Ash extends Tree
{
    public Ash(int age, double height)
    {
        this.age = age;
        this.height = height;
    }
}

或者说实话,它更清楚:

public class Tree
{
    public Tree(int age, double height)
    {
        this.age = age;
        this.height = height;
    }
}

public class Ash extends Tree
{
    public Ash(int age, double height)
    {
        super(age, height);
    }
}

答案 2 :(得分:0)

我附上一些代码以供说明;您在评论中应该理解的“关键字”是大写字母。

我的类使用PACKAGE访问修饰符(实际上没有写入;不同的访问修饰符是PUBLIC,PROTECTED&amp; PRIVATE),因为这允许我在一个源文件中有几个类。

我希望这会有所帮助,你可以从中学到一些东西。如果我能提供帮助,请不要犹豫。


import java.util.ArrayList;


class Forest {

    public ArrayList<Tree> trees = new ArrayList();

    public void initialize () {
        // create and add 3 ashes on 3 different ways
        Ash ash1 = new Ash(10,32.5);    
        Tree ash2 = new Ash(1,2.5); 
        this.trees.add(ash1);       // "this" references the current object, but 
        trees.add(ash2);            // as there is no local trees variable 
        trees.add(new Ash(3,12.0)); // defined the compilier picks the right one 
        trees.add(new Tree(1,1.0));
    }

    public static void main(String[] args) {
        Forest forest = new Forest();
        forest.initialize();
        for (Tree tree : forest.trees) {   // FOR EACH loop
            System.out.println(tree.getClass()+" Age: "+tree.age);  
        }
    }            
}


class Tree {
    public int age;
    public double height;
    public Tree(int age,double height) {    // CONSTRUCTOR - creates an the object
        this.age=age;           // "this" references the current object, so in 
        this.height=height;     // this case the MEMBER VARIABLE is assigned
    }                           // the PARAMETER value
}


class Ash extends Tree {
    public Ash(int age,double height) {
        super(age,height);      // calls the constructor of the SUPERCLASS (Tree
    }                           // in this case with the PARAMETERs
}