简体   繁体   English

使对象数组成为子类

[英]Make object array of sub-class

So it seems like I have to make an object array of a sub-class (Bicycle). 因此,似乎我必须制作一个子类(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 : 更改您的主要方法以创建一个Bicycles数组,然后按索引添加它们:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 父类初始化时创建子类对象 - Create Sub-Class Object when Parent Class is initialized Java:从扩展子类获取对象的最佳方法 - Java: Best way to get object from extended sub-class 反序列化生成的子类结果在父 object - Deserialization of generated sub-class results in the parent object 确保子类的正确性? - Ensure correctness in sub-class? 如何在不创建子类和/或继承类的情况下从另一个类调用Object的方法? - How to call an Object's Method from another class without creating a sub-class and/or inheriting class? 父类定义的方法如何在子类object中被调用? - How does the parent class's defined method get called in a sub-class object? 子类对象的超类引用变量访问子类的属性而不是父类的属性 - Super-class reference variable to a sub-class object accesses the subclass's attribute instead of the super's 创建一个新的对象类或编写一个转换子类对象的方法?或者是其他东西?表现不是偏好 - Create a new object class or write a method which converts sub-class objects? or something else? Performance NOT Preference 为什么类的子类必须是静态的才能在类的构造函数中初始化子类? - Why does a sub-class class of a class have to be static in order to initialize the sub-class in the constructor of the class? 从子类返回数据 - Returning data from a sub-class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM