gpt4 book ai didi

java - 想要静态但不能支持?

转载 作者:行者123 更新时间:2023-12-03 07:37:15 24 4
gpt4 key购买 nike

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

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


return switch 中的声明.有没有想过我可能哪里出错了?
 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;
}
}

最佳答案

我将从这里开始阅读:https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html -> 实际上所有的章节都对你有用:https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
严格来说,要解决“无法从静态上下文中引用”的问题,您可以将类设为静态(汽车、塔科马、摩托车、沃尔沃)static class Car{从我的角度来看,您不需要嵌套类,只需在与 Main 类相同的包中创建类,就可以了(随意创建更多包以更好地构建类)
此外,我假设您的代码正在进行中,因为它存在多个问题:

  • 像这样的方法没有意义public boolean isEngine(boolean engine) {return true;}您收到一个您忽略的参数并返回一个常量值:true;我假设您在这里想要做的是拥有不同类型的汽车,每种汽车都有自己的预定义特征,但为此您应该为父级 Car 中的属性设置值。为此,您可以定义 protected setter,使字段 protected ,或者最好创建采用所有值
  • 的构造函数。
            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 以便返回 null 并得到一个 NPE,我假设工作正在进行
  • 在您的开关中,您正在调用默认构造函数 new Tacoma()但是,由于您定义了带参数的构造函数,因此不再可用,使用可用的构造函数或创建无参数构造函数。

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

    关于java - 想要静态但不能支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65708994/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com