简体   繁体   中英

Make object array of sub-class

So it seems like I have to make an object array of a sub-class (Bicycle).

I then add two objects to this.. and loop the array and print what each object is constructed from.

This sounds thoroughly confusing to me, and I'm unsure how to go about this.

I'll also post the rest of my code, to make more sense.

MAIN:

package javaapplication4;

public class JavaApplication4 {

    public static void main(String[] args) {

        Bicycle myBike = new Bicycle(1, "Haro BMX", true, "Handlebars, Tyres, Frame");
        System.out.println(myBike);        
    }
}


package javaapplication4;

public class Implement {

  String name;
  boolean hasMovingParts;
  String constructedFrom;

  public Implement() {
  }

  public Implement(String name, boolean hasMovingParts, String constructedFrom) {
    this.name = name;
    this.hasMovingParts = hasMovingParts;
    this.constructedFrom= constructedFrom;
  }

  public String getName() {
    return name;
  }

  public boolean getMovingParts() {
    return hasMovingParts;
  }

  public String getConstructedFrom(){
    return constructedFrom;
  }

  public class Bicycle extends Implement {
    public int seatNumber;

  public Bicycle(int seatNumber, String name, boolean hasMovingParts, String constructedFrom) {
    this.seatNumber = seatNumber; //takes the value you pass as parameter
    this.name = name; // and stores it into the instance variable
    this.hasMovingParts = hasMovingParts;
    this.constructedFrom = constructedFrom;   
    }

    @Override
  public String toString(){
      return String.format("*Vehicle Statistics* Seats: %d, Name:" +
           " %s, Contains Moving Parts: %b, Materials: %s",
                seatNumber, name, hasMovingParts, constructedFrom);
  }
}
}


package javaapplication4;

public class Bicycle extends Implement {
  public int seatNumber;

  public Bicycle(int seatNumber, String name, boolean hasMovingParts, String constructedFrom) {
    this.seatNumber = seatNumber;
    this.name = name;
    this.hasMovingParts = hasMovingParts;
    this.constructedFrom = constructedFrom;
  }

  @Override
  public String toString() {
    return String.format("*Vehicle Statistics* Seats: %d, Name:" +
      " %s, Contains Moving Parts: %b, Materials: %s",
      seatNumber, name, hasMovingParts, constructedFrom);
  }
}

Change your main method to create an array of Bicycles, then add them by the index :

public static void main(String[] args) {

    Bicycle[] bicycles = new Bicycle[2];
    bicycles[0] = new Bicycle(1, "Haro BMX", true, "Handlebars, Tyres, Frame");
    bicycles[1] = new Bicycle(1, "Orah XMB", true, "Handlebars, Tyres, Frame");

    for (Bicycle bicycle : bicycles){
        System.out.println(bicycle);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM