有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对car1=新车的静态方法存在问题。创建();

我正在使用Eclipse并试图为课堂作业创建一辆新车。在我的main方法中,Car car1 = new Car.Create();我收到的无法解析为类型错误,但我认为真正的问题是如何在代码中创建Car设置

 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
        }
    }

共 (1) 个答案

  1. # 1 楼答案

    RacingGame中声明了Car和其他类。这使得它们成为内部类。因此,您只能引用RacingGame的实例来实例化它们

    如果将其类声明更改为

    public static class Car
    

    然后Car将是一个静态嵌套类,您可以实例化它,而无需引用RacingGame的实例

    Nested Classes

    或者,将其声明移到RacingGame之外。似乎没有必要把它放在里面