繁体   English   中英

如何在 Java 的构造函数中初始化 String 类型的数组?

[英]How do I initialize an array of type String within a constructor in Java?

In my intermediate Java programming class, we are working through JUnit Tests (I believe this to be the right terminology, forgive me because I'm still very, very green with all the proper terminology) that have us building a Vehicle class that allows us创建具有多个值的车辆类型的 object,同时还具有扩展车辆 class 的选项 class。

我的第一个测试如下:

   void testVehicle()
   {
      Vehicle vehicle = new Vehicle("GMC", 80000, 7995, 4, null);

      assertEquals("GMC", vehicle.getManufacturerName());
      assertEquals(80000, vehicle.getMilesOnVehicle());
      assertEquals(7995, vehicle.getPrice());
      assertEquals(4, vehicle.getNumberOfSeats());
   }

我的课程设置如下:

public class Vehicle
{
    private String ManufacturerName;
    private int MilesOnVehicle;
    private int Price;
    private int NumberOfSeats;
    protected String Options;
    protected String[] Option;
    
    // No arg constructor
    public Vehicle()
    {
        
    }
 
    public Vehicle(String manufacturerName, int miles, int price, int seats, String options[]) {
        ManufacturerName = manufacturerName;
        MilesOnVehicle = miles;
        Price = price;
        NumberOfSeats = seats;
        String Option[] = options;
    }
 
// Getters and setter methods for ManufacturerName, MilesOnVehicle, Price, and NumberOfSeats

}

我的选项 class 如下;

public class Option extends Vehicle
{

    // No arg constructor
    public Option()
    {
    // No arg constructor
    }
    
    public Option(String string)
    {
        Options = string;
    }
    
    /*
     * Method to set option's indexes with String variables
     * set to String variable Option extended from Vehicle class
     */
    public void setDetails(String option)
    {
        Options = option;
    }
    
    /*
     * Method to get option as String
     * after being set by previous method setOption
     */
    public String getDetails() {
        return Options;
    }
    
    public void setOptions(String[] option)
    {
        Option = option;
    }
    
    public Object[] getOptions()
    {
        return null;
    }
    
}

我能够通过第一个测试,但第二个测试是我遇到问题的地方,因为我看到我们为选项引入了两个字符串变量,但我不确定如何 go 关于实现方法或一个适当的构造函数将它们接收,更不用说再次访问它们了。

   @Test
   void testOption()
   {
      Option moonroof = new Option("Moonroof");
       assertEquals("Moonroof", moonroof.getDetails());
      Option leather = new Option("Leather");
      assertEquals("Leather", leather.getDetails());
      Option[] options = { moonroof, leather };
      Vehicle vehicle = new Vehicle("BMW", 90000, 10995, 4, options);
      assertEquals("Moonroof", vehicle.getOptions()[0].getDetails());
      assertEquals("Leather", vehicle.getOptions()[1].getDetails());
   }

我不希望有人为我做功课,而是一些关于我应该做些什么来修复我的错误的指导和指针,以便我了解如何将字符串变量传递到数组中以及如何通过它访问它我必须创建的这两种方法(getOptions 和 getDetails)。

编辑:我现在实际上已经通过了这些挑战的所有测试,并看到了我的错误是什么。 在更仔细地遵循为我们提供的 UML 图之后,我意识到我正在实现比必要更多的方法并创建冗余代码。 正如用户“markspace”所说,没有将 Vehicle 扩展为选项,这是我犯的错误之一。

一些可能会有所帮助的指针,但省略了一些细节供您研究。

选项 class 不应扩展 Vehicle - 它不是 Vehicle 本身。 除了您当前的定义,您还可以将 Option 视为枚举

enum Option{LEATHER, MOONROOF, ...};

或作为抽象 class 选项,以便您可以为每个选项类型添加派生类,并为其颜色、大小等提供字段

// class LeatherOption extends Option
Option leather = new LeatherOption("red);

不要为选项传入数组,而是考虑“...”,然后您可以使用变量 arguments 并省略基本模型的 null 参数:

public Vehicle(String manufacturerName, int miles, int price, int seats, Option ... options) // or String ... options
{ this.options = options; }
=> 
Vehicle vehicle = new Vehicle("GMC", 80000, 7995, 4); // no options specified
Vehicle vehicle = new Vehicle("BMW", 90000, 10995, 4, moonroof, leather);

如果您稍后向 Vehicle 添加更多字段,您最终可能会得到复杂的构造函数 arg 列表,因此最好将构造函数减少到最小参数并设置 setXYZ 以获得额外功能:

vehicle.setOptions(Option ... options)
vehicle.hasOption(Option option) => boolean

暂无
暂无

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

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