java - Add subclass objects to an ArrayList -
I have been a month in my BSc in software, and we are working on this forest project where we have different types There are trees (ash and bee) which grow at different speeds, they are sub-squares of normal class trees.
My problem is now that I have been asked to add two trees of any kind of tree (ash and beech) to an array list. Their Keeping in mind that you already have I hope that helps! Oh, and for the trees - you can probably provide age and height in a constructor parameter, and Can make them something Asha Ash = new ash (5, 1.5); // age, height that Code> public squares will use ash {public ash (interval age, double height) {this.age = age; This.height = height}}} Or honestly, By the way it is clear: int age and
double height should all be different >
Public Square Tree {Public Interval Age; Public double height; Public Zero Growth One Year () {age ++; } Public Zero Show () {System.out.println ("trÃÆ'à ¢ â,¬Å¡Ãƒâ € šÃ, ° + + + +" + "+ "M +" meter "); Public class ash tree (public ash) {extended} {
ash for the source age = 1; height = 1.0;} one year for public zeros () {super.growOneYear (); if (height <15) {height = height * 1.20;}} public zeros show () {System .out.println ("Ask:"); super.show ();}}
tree , at this point you need to store all the trees and sub classes in the
tree
List s's
trees s basically make lists, and add trees to the list.
Public category one {Private list> Lieutenant; Tree> tree; public forest (tree) {tree = new arreelist & lt; & Gt; (); // Diamond syntax Java7, Arreist & Lt; Tree & gt; (6) public zeros addTree (tree tree) on java {tree. Tree (tree); } Public Zero Grows (Bridges) (tree tree: tree) {tree. Tree } // You can tree. Stream (). ForEach (x - & gt; x.growOneYear ()); 8) Public Zero Shows on Java () (for (tree tree: tree) {tree.show ();}}} Public square main square {public static zero main (string [] args) {forest one = new Forest (); ash ashishi = new ash (); middle beetry = new bech (); forest.addTree (ashTree); forest.addTree; forest.show (); forest.growTreesByOneYear (); forest.show ( );
public square tree {public tree (int age, double height) {this.age = age; this.height = height}}} public square ash tree (public ash (Int age, double height) {super (age, height) is extensively; }}
Comments
Post a Comment