2014-10-08 147 views
0

我大約在軟件學士學位一個月,我們正在研究這個森林項目,我們有不同類型的樹木(灰燼和山毛櫸),它們以不同的速度增長。它們是普通類Tree的子類。將子類對象添加到ArrayList中

我的問題是現在我被要求將每種樹(灰和山毛櫸)的兩棵樹添加到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

+0

看來,你會想仿製藥和仿製藥的集合。看這裏:cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/generics-tutorial.pdf – 2014-10-08 14:06:55

回答

1

考慮你已經在類的聲明中extends Tree繼承Tree,在這一點上,所有你需要爲了存儲所有的樹木和Tree小號子類中TreeList 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); 
    } 
} 
+1

非常感謝你!明天再回來看看我是否應該遇到任何後續問題,但這很有道理。再次,非常感謝。 – Aphex 2014-10-13 00:16:12

1

使用繼承。

申報Ash和使用Tree類應該讓你的Tree對象添加到trees集合Beech對象。

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

我附上您說明一些代碼;在評論中你應該理解的「關鍵詞」是大寫字母。

我的類與PACKAGE訪問修飾符(實際上不是寫入;不同的訪問修飾符是PUBLIC,保護& 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 
}