java 不能从静态上下文中引用非静态方法 toString()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13538097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
non-static method toString() cannot be referenced from a static context
提问by WannaBeDroidProgrammer
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. Any suggestions? I would prefer an answer within 2 hours if at all possible.
我不断收到那个烦人的错误。我需要为每个 Room 实例调用 toString 方法。有什么建议?如果可能的话,我希望在 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());
}
}
采纳答案by Hovercraft Full Of Eels
You shouldn't try to call toString()
on a Room class but rather on a Room object. 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.
您不应该尝试调用toString()
Room 类,而是调用Room对象。在该方法中,使用 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]);
}
回答by Vishal
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. So Room.toString() in this case compiler looks for a static method toString. But toString is an instance method so it is causing an issue.
Room 是一个类,而不是一个对象。toString 是一个实例方法。所以在这种情况下,Room.toString() 编译器会寻找一个静态方法 toString。但是 toString 是一个实例方法,所以它会导致一个问题。
Always remember instance methods are called with the object of the class, not with class itself.
永远记住实例方法是用类的对象调用的,而不是类本身。
回答by billybobsteve
What you're probably doing is calling toString()
on the class Room, not an instance of it. For example, instead of writing:
您可能正在做的是调用toString()
Room 类,而不是它的实例。例如,而不是写:
Room.toString()
write:
写:
Room r = new Room()
r.toString()
回答by swim fish
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)