如何在Java的构造函数中初始化String类型的数组?
在我的中间Java编程类中,我们正在进行JUnit测试(我认为这是正确的术语,请原谅我,因为我仍然非常非常熟悉所有正确的术语)这使我们构建了一个Vehicle类,该类允许我们创建一个具有多个值的Vehicle类型的对象,同时还有一个扩展Vehicle类的选项类 我的第一个测试如下:如何在Java的构造函数中初始化String类型的数组?,java,junit,Java,Junit,在我的中间Java编程类中,我们正在进行JUnit测试(我认为这是正确的术语,请原谅我,因为我仍然非常非常熟悉所有正确的术语)这使我们构建了一个Vehicle类,该类允许我们创建一个具有多个值的Vehicle类型的对象,同时还有一个扩展Vehicle类的选项类 我的第一个测试如下: void testVehicle() { Vehicle vehicle = new Vehicle("GMC", 80000, 7995, 4, null);
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
}
我的课程设置如下:
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
}
其中,我的期权类别如下:
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;
}
}
我能够通过第一个测试,但第二个测试是我遇到问题的地方,因为我看到我们为选项引入了两个字符串变量,但我不确定如何实现一个方法或一个适当的构造函数来接收它们,更不用说再次访问它们了
@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”所说的,没有将车辆扩展到选项中,这是我犯的错误之一。一些可能有用的提示,但省略了一些细节供您研究 选项类别不应扩展车辆-它不是车辆本身。除了您当前的定义之外,您还可以考虑选项为EnUM/P>
enum Option{LEATHER, MOONROOF, ...};
或者作为抽象类选项,以便您可以为每个选项类型添加派生类,并为其颜色、大小等设置字段
// class LeatherOption extends Option
Option leather = new LeatherOption("red);
而不是在数组中传递选项,考虑“…”,然后可以使用变量参数,省略基本模型的空参数:
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);
如果以后向车辆添加更多字段,则最终可能会出现复杂的构造函数arg list,因此最好将构造函数减少到最小参数,并使用setXYZ获得额外功能:
vehicle.setOptions(Option ... options)
vehicle.hasOption(Option option) => boolean
鉴于您现在拥有的代码,我认为您应该以不同的方式声明
车辆的ctor。您应该传递选项的数组
而不是字符串的数组
。所以:公共车辆(字符串manufacturerName、int miles、int price、int seats、Option options[])
只是一个建议,调用数组options
和单个字符串Option
。对于类字段也使用camel case。读起来很混乱。