简体   繁体   English

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

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

In my introductory Java course, we are required to create a program that gets and sets info on an array of cars based off what our teacher posted on our class website.在我的 Java 入门课程中,我们需要创建一个程序,该程序根据我们的老师在我们班级网站上发布的内容来获取和设置一系列汽车的信息。 One of the requirements is to make a function that will print the info of all three cars要求之一是创建一个函数来打印所有三辆车的信息

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

The printInfo() function is in another class and looks like: 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.");

Now when I debug it, I get an error message that goes:现在,当我调试它时,我收到一条错误消息:

 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 are the line numbers where the error arises) The error comes from the for loop, but even before I switched this to a loop and just had them all listed outright, I was getting the same error. (31 和 36 是出现错误的行号)错误来自 for 循环,但即使在我将其切换到循环并直接将它们全部列出之前,我也遇到了同样的错误。 I've tried so many things that I'm just beat at this point.我已经尝试了很多事情,在这一点上我只是被打败了。 Someone please help me out.有人请帮帮我。

Here's the main code since it isn't too long:这是主要代码,因为它不太长:

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

And here is the Car class:这是 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."); } }

I've changed some of the variables to static, though I'm not sure it's correct.我已将一些变量更改为静态变量,但我不确定它是否正确。 I've tried the comments but I'm here asking for help because I'm not very good at programming in general.我已经尝试了评论,但我在这里寻求帮助,因为我一般不太擅长编程。 Thank you all for your help and comments.感谢大家的帮助和评论。

In static funcions you can only use other static variables and arguments that were passed to this functions.在静态函数中,您只能使用传递给此函数的其他静态变量和参数。 There is no this context.没有this上下文。

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

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