想要静态但不能支持?

Wants static but can't support?

我正在尝试创建一个也显示信息的随机汽车发电机。我以为我拥有一切,直到 randomCar 部分。它说

'com.company.Main.this' cannot be referenced from a static context

switch中的return语句下。有没有想过我可能哪里出错了?

 package com.company;
 
public class Main {
 
    class Car{
        private String name;
        private boolean engine;
        private int cylinders;
        private int wheels;
 
        public Car(String name){
            this.name = name;
        }
 
        public String getName(){
            return name;
        }
 
 
        public int getCylinders() {
            if(cylinders == 0){
                System.out.println("Unknown amount of cylinders");
            }
            return cylinders;
        }
 
        public int getWheels() {
            return wheels;
        }
 
        public boolean isEngine() {
            return engine;
        }
    }
 
    class Tacoma extends Car{
 
        public Tacoma(String name) {
            super("Tacoma");
        }
 
 
        public boolean isEngine(boolean engine) {
            return true;
        }
 
 
        public int getCylinders(int cylinders) {
            return 6;
        }
 
 
        public int getWheels(int wheels) {
            return 4;
        }
    }
 
    class Motorcycle extends Car{
 
        public Motorcycle(String name) {
            super("Harley Davidson");
        }
 
 
        public boolean isEngine(boolean engine) {
            return true;
        }
 
 
        public int getCylinders(int cylinders) {
            return 2;
        }
 
 
        public int getWheels(int wheels) {
            return 2;
        }
    }
 
    class Volvo extends Car{
        public Volvo(String name) {
            super("Volvo");
        }
 
 
        public boolean isEngine(boolean engine) {
            return true;
        }
 
        public int getCylinders(int cylinders) {
            return 4;
        }
 
 
        public int getWheels(int wheels) {
            return 4;
        }
    }
 
 
 
    public static void main(String[] args) {
   for (int i = 1; i<6; i++){
       Car car = randomCar();
        System.out.println("Car # " + i + ":" + car.getName() + "\n" +
                "Number of cylinders: " + car.getCylinders() + "\n" +
                "Number of wheels: " + car.getWheels()+ "\n" +
                "Engine is: " + car.isEngine());
       }
    }
 
    private static Car randomCar() {
        int randomNumber = (int) (Math.random()*5) +1;
        System.out.println("Random number generated is: " + randomNumber);
        switch (randomNumber){
            case 1:
                return new Tacoma(); // This is where I am getting an error
            case 2:
                return new Motorcycle(); // This is where I am getting an error
            case 3:
                return new Volvo(); // This is where I am getting an error
        }
        return null;
    }
}

当您将 Car class 定义放在 Main 的 class 定义中时,您使 Car 成为内部 class,因此 Car 需要外部 class主要实例。在静态方法中没有 Main 实例,没有它就无法创建 Car。

有一个直接的解决方法:将关键字 static 添加到 Car class:

static class Car { 

这意味着没有 link 到封闭对象。

但是将其设为嵌套 class 没有任何好处,最好不要在开始时将一个 class 定义放在另一个定义中。

您定义的内部 类 是 instance 成员,这意味着它们属于 Main 的特定实例,因此不能从中引用没有 Main 实例的 static 上下文。解决这个问题的最简单方法是声明所有内部 类 static.

首先,要解决你的错误:'com.company.Main.this' cannot be referenced from a static context,将所有方法设为静态:

static class Car{//code here} 
static class Volvo extends Car{//code here}
static class Tacoma extends Car{//code here}
static class Motorcycle extends Car{//code here}

只要您看到该错误,就意味着一个静态方法正在调用一个非静态方法。因此,只需将两者设为非静态或设为静态即可。唯一的例外是 public static void main(String[] args);,其中 必须 static.

解决了原来的错误后,还有更多需要调试的地方:

'Volvo(java.lang.String)' in 'com.company.Main.Volvo' cannot be applied to '()'

'Motorcycle(java.lang.String)' in 'com.company.Main.Motorcycle' cannot be applied to '()'

'Tacoma(java.lang.String)' in 'com.company.Main.Tacoma' cannot be applied to '()'

这意味着您的方法 Tacoma()Volvo()Motorcycle() 需要参数 String name。所以你所要做的就是给他们一个名字:在这里,它是

`new Tacoma("cool")`



new Volvo("car")



new Motorcycle("harley davidson")`

最后,在解决了静态和参数问题之后,您得到了一个 NullPointerException,因为 randomCar() return 为 null。您的方法 Car randomCar(),表示它将 return 一个 Car,但是 return 语句是 return null;。因此,这里只是return一个Car-rtn

private static Car randomCar() {
        int randomNumber = (int) (Math.random()*5) +1;
        System.out.println("Random number generated is: " + randomNumber);
        Car rtn = null;
        switch (randomNumber){
            case 1:
                rtn =  new Tacoma("cool"); // This is where I am getting an error
            case 2:
                rtn =  new Motorcycle("harley davidson"); // This is where I am getting an error
            case 3:
                rtn = new Volvo("car"); // This is where I am getting an error
        }
        return rtn;
    }

这不是您的代码所需的全部调试,但这只是一个开始:这是系统到目前为止所做的事情:

Random number generated is: 3
Unknown amount of cylinders
Car # 1:Volvo
Number of cylinders: 0
Number of wheels: 0
Engine is: false
Random number generated is: 5

万岁! 这有帮助吗?

我将从这里开始阅读:https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html -> actually all the chapters there would be useful for you to read: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

严格来说,要解决“无法从静态上下文中引用”的问题,您只需将 classes 设为静态(汽车、塔科马、摩托车、沃尔沃)static class Car{

从我的角度来看,您不需要嵌套的 classes,只需在与 Main class 相同的包中创建 classes 就可以了go(随意创建更多包以更好地构建您的 classes)

此外,我假设您的代码还在开发中,因为它存在多个问题:

  • 像这样的方法没有意义public boolean isEngine(boolean engine) {return true;}您收到一个您忽略的参数并且您return一个常量值:true;我假设您想在这里做的是拥有不同类型的汽车,每种汽车都有自己的预定义特征,但为此您应该在父项 Car 中设置属性值。为此,您要么定义受保护的设置器,使字段受保护,要么最好创建采用所有值的构造函数
        public Car(String name, boolean engine, int cylinders, int wheels) {
            this.name = name;
            this.engine = engine;
            this.cylinders = cylinders;
            this.wheels = wheels;
        }

你可以在塔科马

        public Tacoma(String name) {
            super(name, true, 6, 4);
        }
  • 运行 你的代码我得到了 randomNumber 5 所以 returned null 并且得到了一个 NPE,我假设工作正在进行中
  • 在您的 switch 中,您正在调用默认构造函数 new Tacoma() 但是它不再可用,因为您定义了一个带参数的构造函数,请使用可用的构造函数或创建无参数构造函数。

关于 OOP 原则还有其他问题,所以我建议再次阅读它们,只是 google“javaOOP 原则”,然后是“SOLID”...有很多很棒的资源在那里,你只需要时间和耐心,你就会到达那里!