繁体   English   中英

所以我很难理解什么时候使用静态以及什么时候从函数头中排除它

[英]So I'm having a hard time understanding when to use static and when to exclude it from function headers

在我的 Java 入门课程中,我们需要创建一个程序,该程序根据我们的老师在我们班级网站上发布的内容来获取和设置一系列汽车的信息。 要求之一是创建一个函数来打印所有三辆车的信息

 public static void printAllCarsInfo() // function for printing info needed for all three cars { for ( int index = 0 ; index < NUM_CARS ; index++ ) cars[index].printInfo();

printInfo() 函数在另一个类中,如下所示:

 public void printInfo() { if ( fuelGallons > 0 ) System.out.println("The " + getModel() + " has driven " + getMilesDriven() + " miles and has " + getFuelGallons() + " gallons left."); else System.out.println("The " + getModel() + " has driven " + getMilesDriven() + " miles and is out of gas.");

现在,当我调试它时,我收到一条错误消息:

 Smith12.java:31: error: cannot find symbol cars[index].printInfo(); ^ symbol: variable cars location: class Smith12 Smith12.java:36: error: cannot find symbol cars[index].drive(miles); ^ symbol: variable cars location: class Smith12 2 errors

(31 和 36 是出现错误的行号)错误来自 for 循环,但即使在我将其切换到循环并直接将它们全部列出之前,我也遇到了同样的错误。 我已经尝试了很多事情,在这一点上我只是被打败了。 有人请帮帮我。

这是主要代码,因为它不太长:

 import java.util.Scanner; public class Smith12 { public static final int NUM_CARS = 3; public static void main(String[] args) { Car[] cars = new Car[NUM_CARS]; cars[0] = new Car( "Toyota Camry", 3400 ); cars[1] = new Car( "Ford F-150", 5000 ); cars[2] = new Car( "Honda Civic", 3000 ); for ( int index = 0 ; index < NUM_CARS ; index++ ) cars[index].drive(10); printAllCarsInfo(); cars[0].setFuelGallons(5); Scanner scanner = new Scanner(System.in); System.out.println("How many miles is your road trip? "); double roadTrip = scanner.nextDouble(); driveAllCars( 10 ); printAllCarsInfo(); } public static void printAllCarsInfo() // function for printing info needed for all three cars { for ( int index = 0 ; index < NUM_CARS ; index++ ) Car[index].printInfo(); } public static void driveAllCars( double miles ) { for ( int index = 0 ; index < NUM_CARS ; index++ ) cars[index].drive(miles); } }

这是 Car 类:

 public class Car { private static String model; private static double mpg; private static double milesDriven; private static double fuelGallons; public Car( String carModel, double weight ) { model = carModel; if ( weight > 4000 ) mpg = 20.0; else mpg = 30.0; milesDriven = 7; fuelGallons = 15; } public String getModel() { return model; } public double getMPG() { return mpg; } public double getMilesDriven() { return milesDriven; } public double getFuelGallons() { return fuelGallons; } public void setFuelGallons( double gallons ) { fuelGallons += gallons; } public void setMilesDriven( double distance ) { milesDriven += distance; } public double getMilesLeft() { return mpg * fuelGallons; } public void drive( double miles ) { if ( miles <= getMilesLeft() ) { milesDriven += miles; fuelGallons -= ( miles / mpg ); } else { milesDriven += getMilesLeft(); fuelGallons = 0; } } public void printInfo() { if ( fuelGallons > 0 ) System.out.println("The " + this.getModel() + " has driven " + this.getMilesDriven() + " miles and has " + this.getFuelGallons() + " gallons left."); else System.out.println("The " + this.getModel() + " has driven " + this.getMilesDriven() + " miles and is out of gas."); } }

我已将一些变量更改为静态变量,但我不确定它是否正确。 我已经尝试了评论,但我在这里寻求帮助,因为我一般不太擅长编程。 感谢大家的帮助和评论。

在静态函数中,您只能使用传递给此函数的其他静态变量和参数。 没有this上下文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM