Java 的产量翻了一番
output is doubling in Java
谁能帮我弄清楚为什么我的输出翻倍了?正如您在输出中看到的那样,它打印出马力、扭矩、压缩比、燃油经济性和推荐燃油的零值。然后我制作了一个 toString 来打印出正确的数字。星号内的数字是我想删除或打印出正确字符串的数字。这个项目让我们使用继承和多态性,所以我对此还很陌生。我的输出是:
engine = Hybrid 8 Cyclinders
***horsepower = 0; torque = 0
compressionRatio = 0; fuelEconomy = 0
fuelRecommended = 0***
transmission = Electronically controlled Continuously Variable Transmission (Standard Verison)
horsepower = 98 hp @ 5200 rpm
Fuel Recommended = 85 Octane
Compression Ratio = 13.0:1
torque = 153 lb-ft
Fuel Economy = 51 City / 48 Highway
Fuel Recommended = 85 Octane
battery = null
我的主要 class 是:
public class AutoTest
public static void main(String[] args)
{
Automobile automobile;
Prius prius = new Prius("Toyota", "Prius", "Hybrid", "Automatic", "98 hp @ 5200 rpm",
"153 lb-ft", "13.0:1", "51 City / 48 Highway", "85 Octane");
automobile = prius;
System.out.println("Automobile: " + automobile);
}
}
汽车是 class 得到扩展的:
public class Automobile
{
Automobile(String make, String model, String engine, String transmission,
String horsepower, String torque, String compressionRatio, String fuelEconomy,
String fuelRecommended)// setting make, model, engine and transmission
{
setMake(make);
setModel(model);
setEngine(engine);
setTransmission(transmission);
setHorsepower(horsepower);
setTorque(torque);
setCompressionRatio(compressionRatio);
setFuelEconomy(fuelEconomy);
setFuelRecommended(fuelRecommended);
}// end of constructor Automobile
public String getMake()
{
return this.make;
}
public void setMake(String make)
{
this.make = make;
}
public String getModel()
{
return this.model;
}
public void setModel(String model)
{
this.model = model;
}
public Engine getEngine()
{
return this.engine;
}
public void setEngine(Engine engine)
{
this.engine = engine;
}
public Transmission getTransmission()
{
return this.transmission;
}
public void setTransmission(Transmission transmission)
{
this.transmission = transmission;
}
public String getHorsepower()
{
return this.horsepower;
}
public void setHorsepower(String horsepower)
{
this.horsepower = horsepower;
}
public String getTorque()
{
return this.torque;
}
public void setTorque(String torque)
{
this.torque = torque;
}
public String getCompressionRatio()
{
return this.compressionRatio;
}
public void setCompressionRatio(String compressionRatio)
{
this.compressionRatio = compressionRatio;
}
public String getFuelEconomy()
{
return this.fuelEconomy;
}
public void setFuelEconomy(String fuelEconomy)
{
this.fuelEconomy = fuelEconomy;
}
public String getFuelRecommended()
{
return this.fuelRecommended;
}
public void setFuelRecommended(String fuelRecommended)
{
this.fuelRecommended = fuelRecommended;
}
public void setEngine(String engine)// setEngine by using switch loop to
// invoke the classes that extend from
// engine
{
switch (engine) {
case "Hybrid":
setEngine(new Hybrid());
break;
case "Gasoline":
setEngine(new Gasoline());
break;
default:
System.out.println("Invalid engine entry: " + engine);
}// end of switch loop for engine
}// end of setEngine switch loop
public void setTransmission(String transmission)// setTransmission by using a
// switch loop to invoke the
// classes that extend from
// transmission
{
switch (transmission) {
case "eCVT":
setTransmission(new Ecvt());
break;
case "Automatic":
setTransmission(new Automatic());
break;
default:
System.out.println("Invalid transmission entry:" + transmission);
}// end of switch loop for transmission
}// end of setTransmission switch loop
public String toString()
{
return "\nmake = " + make + "; model = " + model + "\nengine = " + engine
+ "\ntransmission = " + transmission + "\nhorsepower = " + horsepower +
"\nFuel Recommended = " + fuelRecommended +
"\nCompression Ratio = " + compressionRatio +"\ntorque = " + torque +
"\nFuel Economy = " + fuelEconomy + "\nFuel Recommended = " + fuelRecommended;
}// end of String toString to print make, model, engine and transmission for
// car
private String make;
private String model;
private Engine engine;
private Transmission transmission;
private String horsepower;
private String torque;
private String compressionRatio;
private String fuelEconomy;
private String fuelRecommended;
}// end of class Automobile
我的普锐斯 class 是:
public class Prius extends Automobile
{
public Prius(String make, String model, String engine, String transmission,
String horsepower, String torque, String compressionRatio,
String fuelEconomy, String fuelRecommended)
{
super(make, model, engine, transmission, horsepower, torque,
compressionRatio, fuelEconomy, fuelRecommended);
}
public String getBattery()
{
return this.battery;
}
public void setBattery(String battery)
{
this.battery = battery;
}
@Override
public String toString()
{
return super.toString() + "\nbattery = " + battery;
}
private String battery;
}
我的引擎 class 看起来像:
public class Engine
{
Engine(String engineType, int numCylinders)
{
setNumCylinders(numCylinders);
setEngineType(engineType);
}
public String getEngineType()
{
return this.engineType;
}
public void setEngineType(String engineType)
{
this.engineType = engineType;
}
public int getNumCylinders()
{
return numCylinders;
}
public void setNumCylinders(int numCylinders)
{
this.numCylinders = numCylinders;
}
public String toString()
{
return "" + engineType + " " + numCylinders + " Cyclinders ";
}
private String engineType;
private int numCylinders;
}
我很确定不需要的字段属于 class 引擎
public void setEngine(String engine)// setEngine by using switch loop to
// invoke the classes that extend from
// engine
{
switch (engine) {
case "Hybrid":
setEngine(new Hybrid());
break;
case "Gasoline":
setEngine(new Gasoline());
break;
default:
System.out.println("Invalid engine entry: " + engine);
}// end of switch loop for engine
}// end of setEngine switch loop
这使用默认/未初始化参数分配引擎,它们由汽车的 toString() 方法打印
像这样调用了 toString 方法 引擎和传输方法 toString() 被调用
public String toString()
{
return "\nmake = " + make + "; model = " + model + "\nengine = " + engine.toString()
+ "\ntransmission = " + transmission.toString() + "\nhorsepower = " + horsepower +
"\nFuel Recommended = " + fuelRecommended +
"\nCompression Ratio = " + compressionRatio +"\ntorque = " + torque +
"\nFuel Economy = " + fuelEconomy + "\nFuel Recommended = " + fuelRecommended;
}// end of String toString to print make, model, engine and transmission for
// car
谁能帮我弄清楚为什么我的输出翻倍了?正如您在输出中看到的那样,它打印出马力、扭矩、压缩比、燃油经济性和推荐燃油的零值。然后我制作了一个 toString 来打印出正确的数字。星号内的数字是我想删除或打印出正确字符串的数字。这个项目让我们使用继承和多态性,所以我对此还很陌生。我的输出是:
engine = Hybrid 8 Cyclinders
***horsepower = 0; torque = 0
compressionRatio = 0; fuelEconomy = 0
fuelRecommended = 0***
transmission = Electronically controlled Continuously Variable Transmission (Standard Verison)
horsepower = 98 hp @ 5200 rpm
Fuel Recommended = 85 Octane
Compression Ratio = 13.0:1
torque = 153 lb-ft
Fuel Economy = 51 City / 48 Highway
Fuel Recommended = 85 Octane
battery = null
我的主要 class 是:
public class AutoTest
public static void main(String[] args)
{
Automobile automobile;
Prius prius = new Prius("Toyota", "Prius", "Hybrid", "Automatic", "98 hp @ 5200 rpm",
"153 lb-ft", "13.0:1", "51 City / 48 Highway", "85 Octane");
automobile = prius;
System.out.println("Automobile: " + automobile);
}
}
汽车是 class 得到扩展的:
public class Automobile
{
Automobile(String make, String model, String engine, String transmission,
String horsepower, String torque, String compressionRatio, String fuelEconomy,
String fuelRecommended)// setting make, model, engine and transmission
{
setMake(make);
setModel(model);
setEngine(engine);
setTransmission(transmission);
setHorsepower(horsepower);
setTorque(torque);
setCompressionRatio(compressionRatio);
setFuelEconomy(fuelEconomy);
setFuelRecommended(fuelRecommended);
}// end of constructor Automobile
public String getMake()
{
return this.make;
}
public void setMake(String make)
{
this.make = make;
}
public String getModel()
{
return this.model;
}
public void setModel(String model)
{
this.model = model;
}
public Engine getEngine()
{
return this.engine;
}
public void setEngine(Engine engine)
{
this.engine = engine;
}
public Transmission getTransmission()
{
return this.transmission;
}
public void setTransmission(Transmission transmission)
{
this.transmission = transmission;
}
public String getHorsepower()
{
return this.horsepower;
}
public void setHorsepower(String horsepower)
{
this.horsepower = horsepower;
}
public String getTorque()
{
return this.torque;
}
public void setTorque(String torque)
{
this.torque = torque;
}
public String getCompressionRatio()
{
return this.compressionRatio;
}
public void setCompressionRatio(String compressionRatio)
{
this.compressionRatio = compressionRatio;
}
public String getFuelEconomy()
{
return this.fuelEconomy;
}
public void setFuelEconomy(String fuelEconomy)
{
this.fuelEconomy = fuelEconomy;
}
public String getFuelRecommended()
{
return this.fuelRecommended;
}
public void setFuelRecommended(String fuelRecommended)
{
this.fuelRecommended = fuelRecommended;
}
public void setEngine(String engine)// setEngine by using switch loop to
// invoke the classes that extend from
// engine
{
switch (engine) {
case "Hybrid":
setEngine(new Hybrid());
break;
case "Gasoline":
setEngine(new Gasoline());
break;
default:
System.out.println("Invalid engine entry: " + engine);
}// end of switch loop for engine
}// end of setEngine switch loop
public void setTransmission(String transmission)// setTransmission by using a
// switch loop to invoke the
// classes that extend from
// transmission
{
switch (transmission) {
case "eCVT":
setTransmission(new Ecvt());
break;
case "Automatic":
setTransmission(new Automatic());
break;
default:
System.out.println("Invalid transmission entry:" + transmission);
}// end of switch loop for transmission
}// end of setTransmission switch loop
public String toString()
{
return "\nmake = " + make + "; model = " + model + "\nengine = " + engine
+ "\ntransmission = " + transmission + "\nhorsepower = " + horsepower +
"\nFuel Recommended = " + fuelRecommended +
"\nCompression Ratio = " + compressionRatio +"\ntorque = " + torque +
"\nFuel Economy = " + fuelEconomy + "\nFuel Recommended = " + fuelRecommended;
}// end of String toString to print make, model, engine and transmission for
// car
private String make;
private String model;
private Engine engine;
private Transmission transmission;
private String horsepower;
private String torque;
private String compressionRatio;
private String fuelEconomy;
private String fuelRecommended;
}// end of class Automobile
我的普锐斯 class 是:
public class Prius extends Automobile
{
public Prius(String make, String model, String engine, String transmission,
String horsepower, String torque, String compressionRatio,
String fuelEconomy, String fuelRecommended)
{
super(make, model, engine, transmission, horsepower, torque,
compressionRatio, fuelEconomy, fuelRecommended);
}
public String getBattery()
{
return this.battery;
}
public void setBattery(String battery)
{
this.battery = battery;
}
@Override
public String toString()
{
return super.toString() + "\nbattery = " + battery;
}
private String battery;
}
我的引擎 class 看起来像:
public class Engine
{
Engine(String engineType, int numCylinders)
{
setNumCylinders(numCylinders);
setEngineType(engineType);
}
public String getEngineType()
{
return this.engineType;
}
public void setEngineType(String engineType)
{
this.engineType = engineType;
}
public int getNumCylinders()
{
return numCylinders;
}
public void setNumCylinders(int numCylinders)
{
this.numCylinders = numCylinders;
}
public String toString()
{
return "" + engineType + " " + numCylinders + " Cyclinders ";
}
private String engineType;
private int numCylinders;
}
我很确定不需要的字段属于 class 引擎
public void setEngine(String engine)// setEngine by using switch loop to
// invoke the classes that extend from
// engine
{
switch (engine) {
case "Hybrid":
setEngine(new Hybrid());
break;
case "Gasoline":
setEngine(new Gasoline());
break;
default:
System.out.println("Invalid engine entry: " + engine);
}// end of switch loop for engine
}// end of setEngine switch loop
这使用默认/未初始化参数分配引擎,它们由汽车的 toString() 方法打印
像这样调用了 toString 方法 引擎和传输方法 toString() 被调用
public String toString()
{
return "\nmake = " + make + "; model = " + model + "\nengine = " + engine.toString()
+ "\ntransmission = " + transmission.toString() + "\nhorsepower = " + horsepower +
"\nFuel Recommended = " + fuelRecommended +
"\nCompression Ratio = " + compressionRatio +"\ntorque = " + torque +
"\nFuel Economy = " + fuelEconomy + "\nFuel Recommended = " + fuelRecommended;
}// end of String toString to print make, model, engine and transmission for
// car