我正在使用Eclipse并尝试为课堂作业创建一辆新车。在我的主要方法中,Car car1 = new Car.Create();
我收到了无法解决类型错误的错误,但我认为真正的问题是代码中如何创建汽车。
public static Car Create()
{
return new Car();
}
我尝试将其作为静态删除,但这样做时会收到其他错误。
package cargame;
public class RacingGame
{
public static void main(String[] args)
{
Car car1 = new Car.Create();
car1.StartEngine();
car1.PumpUpTheTires();
car1.StartEngine();
car1.setVelocity(-1);
car1.setVelocity(61);
car1.setVelocity(55);
car1.StopEngine();
car1.StartEngine();
car1.Restart();
System.in.read();
}
public class Car
{
private int Velocity;
public final int getVelocity()
{
return Velocity;
}
private Engine Engine;
public final Engine getEngine()
{
return Engine;
}
private void setEngine(Engine value)
{
Engine = value;
}
private Tire[] Tires;
public final Tire[] getTires()
{
return Tires;
}
private void setTires(Tire[] value)
{
Tires = value;
}
private Car()
{
this.setEngine(new Engine());
this.setTires(new Tire[4]);
Tire tempVar = new Tire();
tempVar.setPSI(0);
this.getTires()[0] = tempVar;
Tire tempVar2 = new Tire();
tempVar2.setPSI(1);
this.getTires()[1] = tempVar2;
Tire tempVar3 = new Tire();
tempVar3.setPSI(2);
this.getTires()[2] = tempVar3;
Tire tempVar4 = new Tire();
tempVar4.setPSI(3);
this.getTires()[3] = tempVar4;
}
public static Car Create()
{
return new Car();
}
public final void PumpUpTheTires()
{
System.out.println("Pumping Tires");
for (Tire tire : getTires())
{
tire.setPSI(33);
}
}
public final void Report()
{
System.out.println("I am a car");
}
public final void StartEngine()
{
boolean areTiresFull = AreTiresFull();
if (areTiresFull == true)
{
System.out.println("I am the car starting the engine.");
this.getEngine().Start();
return;
}
System.out.println("I was unable to start the car. Check the tires!");
}
public final void Restart()
{
this.StopEngine();
this.StartEngine();
}
public final void StopEngine()
{
this.getEngine().Stop();
this.setVelocity(0);
}
private boolean AreTiresFull()
{
for (Tire tire : getTires())
{
if (tire.getPSI() < 32)
{
return false;
}
}
return true;
}
public final void setVelocity(int value)
{
if (value < 0)
{
System.out.println("You cannot set the speed to " + value + "! That is too low!");
return;
}
if (value > 60)
{
System.out.println("You cannot set the speed to " + value + "! That is too high!");
return;
}
this.setVelocity(value);
System.out.println("Velocity set to " + value);
}
}
public class Tire
{
private int PSI;
public final int getPSI()
{
return PSI;
}
public final void setPSI(int value)
{
PSI = value;
}
}
public class Engine
{
private EngineState State = EngineState.values()[0];
public final EngineState getState()
{
return State;
}
public final void setState(EngineState value)
{
State = value;
}
public Engine()
{
this.setState(EngineState.Default);
}
public final void Start()
{
this.setState(EngineState.Started);
System.out.println("Engine Started!");
}
public final void Stop()
{
this.State = EngineState.Stopped;
System.out.println("Engine Stopped!");
}
public final void Report()
{
System.out.println("I'm an engine");
}
}
public enum EngineState
{
Default,
Started,
Stopped
}
}
答案 0 :(得分:2)
您有Car
和其他类在内部声明为 RacingGame
。这使他们成为内部类。因此,您只能参考RacingGame
的实例来实例化它们。
如果将其类声明更改为
public static class Car
然后Car
将是一个静态嵌套类,您可以实例化它而无需引用RacingGame
的实例。
请参见Nested Classes。
或者,将其声明移到RacingGame
之外。似乎没有必要将其包含在其中。