输出在Java中翻倍

时间:2015-07-04 21:31:36

标签: java inheritance polymorphism

有人可以帮我弄清楚为什么我的输出会翻倍吗?正如您在输出中所看到的那样,它为马力,扭矩,压缩比,燃油经济性和推荐的燃油打印出零。然后我创建了一个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

我的主要课程是:

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


}
}

汽车是扩展的类:

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

我的prius课程是:

 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;

 }

我的引擎类看起来像:

  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;

  }

1 个答案:

答案 0 :(得分:0)

我很确定不需要的字段属于类引擎

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