简体   繁体   English

无法从 static 上下文中引用非静态方法 toString()

[英]non-static method toString() cannot be referenced from a static context

Don't want any code, just want some sort of guidance.不需要任何代码,只需要某种指导。 Would like to keep my academic integrity in tact;)想保持我的学术诚信;)

I keep getting that annoying error.我不断收到那个烦人的错误。 I need to call the toString method for each Room instance.我需要为每个 Room 实例调用 toString 方法。 Any suggestions?有什么建议么? I would prefer an answer within 2 hours if at all possible.如果可能的话,我希望在 2 小时内得到答复。

public class Hotel
{
    //constant
    public static final int NUM_ROOMS = 20;

    //variables
    public Room[] theRoom;
    public String name;
    public int totalDays;
    public double totalRate;
    public int singleCount;
    public int doubleCount;
    public int roomsRented;
    public int NOT_FOUND;

    public Hotel(String newName) {
        name = newName;
        Room[] Rooms = new Room[NUM_ROOMS];
    }

    public double getTotalRentalSales() {
        return totalRate + roomsRented;
    }

    public double getAvgDays() {
        return roomsRented/totalDays;
    }

    public double getAvgRate() {
        return totalRate/roomsRented;
    }

    public int getSingleCount() {
        return singleCount;
    }

    public int getDoubleCount() {
        return doubleCount;
    }

    public String printRentalList() {
        System.out.println("Room Information: " + Room.toString());
    }
}

You shouldn't try to call toString() on a Room class but rather on a Room object .您不应该尝试在 Room 类上调用toString() ,而是在 Room对象上调用。 In that method, loop through the array of rooms with a for loop and print the String returned by calling toString() for each Room object held in the array since this is what it looks like your method should do.在该方法中,使用 for 循环遍历房间数组,并打印通过调用toString()为数组中保存的每个 Room 对象返回的字符串,因为这是您的方法应该执行的操作。

For example例如

System.out.println("All Foos held here include: ");

// using a "for-each" loop, assuming an array called fooArray that holds Foo objects
for (Foo foo: fooArray) {
   System.out.println(foo);
}

You will obviously have to change the types and variable names for your code.您显然必须更改代码的类型和变量名称。

Edit 2: although you will have to use a standard for loop, not a for-each loop, since you won't be looping through the entire array, but rather will quit when roomsRented count is reached.编辑 2:虽然您将不得不使用标准 for 循环,而不是 for-each 循环,因为您不会循环整个数组,而是会在达到 roomsRented 计数时退出。

System.out.println("All Foos held here include: ");

// using standard for loop, assuming an array called fooArray that holds Foo objects
for (int i = 0; i < someMaxNumber; i++) {
   System.out.println(fooArray[i]);
}
System.out.println(Arrays.toString(arr));

In this line when you are writing arrays a dropdown menu will appear, there you will see the default package or the java.util package. You need to select the java.util package and the problem will be solved.在这一行中,当你写 arrays 时,会出现一个下拉菜单,在那里你会看到默认的 package 或java.util package。你需要 select java.util 884373

As error is already states, do not call instance method in static context.由于错误已经说明,请勿在静态上下文中调用实例方法。

Room is a class, not an object.房间是一个类,而不是一个对象。 toString is a instance method. toString 是一个实例方法。 So Room.toString() in this case compiler looks for a static method toString.所以 Room.toString() 在这种情况下编译器会寻找一个静态方法 toString。 But toString is an instance method so it is causing an issue.但是 toString 是一个实例方法,所以它会导致问题。

Always remember instance methods are called with the object of the class, not with class itself.永远记住实例方法是用类的对象调用的,而不是类本身。

What you're probably doing is calling toString() on the class Room, not an instance of it.您可能正在做的是在类 Room 上调用toString() ,而不是它的实例。 For example, instead of writing:例如,不要写:

Room.toString()

write:写:

Room r = new Room()
r.toString()

Look at the following code, you can compile toString with a static variable without a new object, it just throw exception at run time看下面的代码,你可以用静态变量编译toString,不需要新对象,它只是在运行时抛出异常

demo>cat Test.java 
class Water {
  public String toString() {return "whatever";}
}

public class Test { 
  static Water water;
  public static void main(String...args) {
    System.out.println(water.toString());
  }
}

demo>javac Test.java 
demo>java Test
Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:8)

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

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