java - 不能从静态上下文中引用非静态方法

标签 java non-static

Possible Duplicate:
calling non-static method in static method in Java

当我尝试调用涉及主函数中已定义对象的方法时,我遇到了“无法从静态上下文引用非静态方法”错误。

我有三个类:一个 Person 类、一个 Book 类和一个包含 main 方法的第三个类。

现在,我被要求在 main() 中创建四个 Person 实例,并定义一个方法“doesAnybodyWantIt(Book b)”,询问这四个人是否想要这本书。它看起来像这样:doesAnybodyWantIt(Book b){ me.doIwantIt(b); lisa.doIwantIt(b).. with the four person-objects).

正如我担心的那样,javac 不知道四个 Person 对象存在并引发错误。我知道我可以将方法编写为 doesAnybodyWantIt(Book b, Person a, ... ),但这不是我被要求的。

所以我的问题是:有没有办法回避这个问题,而不需要将 Persons 作为参数传递给 doesAnybodyWantIt -方法?我试图包括 Person me = new Person(..)在构造函数方法中没有运气。

在这种类型的设置中,我想对我知道我将定义的对象执行特定的操作,我如何避免错误?

我不确定是否应该包含原始源代码;所有方法和参数都给出了挪威语名称,因此我担心它的可读性不是很好。

非常感谢大家! 马吕斯

谢谢各位的回复!你当然是对的,我必须在这个困惑的问题中包含一些代码。

class Error{
private Person jeg;

public void doesAnybodyWantIt(Bok b){  //This method has to be declaired withink the classm, and can only take the Bok b as a parameter. 
Bok bok = jeg.vilJegHaBoka(b);
// loops over the four persons in question. 
}


public static void main(String[] args){
Person jeg = new Person("Jeg", "java");
Person lisa = new Person("Lisa", "crime");
Person ramiz = new Person("Ramiz", "food");
Person emil = new Person("Emil", "sports");
doesAnybodyWantIt(new Bok("java"));  
}


}



class Bok{
private String kat;

Bok(String k){ //the category of the book is assigned to kat as a string. 
kat = k;
}

public String kategori(){return kat;} //returns the category.


}



class Person{
private String navn;  //name
// private Person bestevenn; non-relevant
private String bokKat; // The category of books that is at interest to the person.

Person(String navnet, String kat){   //sets the name and category of books. 
navn = navnet;
bokKat = kat;
}



private boolean mittInteressefelt(Bok b){  //Tests if the book's category is the same as my interest. 
if (bokKat.equals(b.kategori())){        //Returns true if interested. else false.
return true;
}
else{ 
return false;
}
}


public Bok vilJegHaBoka(Bok b){              //A method to decide if the book will be kept or not. Returns null if i want the book, and the book otherwise. 
if (mittInteressefelt(b) == true){  return null; }
else { return b;}

}
}

一个人有名字和兴趣领域,一本书有一个类别。作业说我应该创建一个方法 vilNoenHaBoka (= doesAnybodyWantTheBook) 循环遍历 main 中创建的四个特定的 Person 对象,并询问每个人是否想要这本书。它还指出该方法应该类似于 public void vilNoenHaBoka(Bok b)。现在,我对问这个明显与作业相关的问题感到非常抱歉,如果不合适,请告诉我。然而,我只是好奇地想找出解决此类问题的好方法 - 例如,我应该将 Person 对象传递给 vilNoenHaBoka() 吗?

再次感谢!

最佳答案

我认为除了 main() 方法之外,您不需要任何静态上下文。相反,您应该询问每个Person是否想要一本书,例如

List<Person> persons = ... // and populate your list
for (Person p : persons) {
   if (p.wantsBook(book)) {
      ...
   }
}

面向对象的关键是你告诉对象为你做事,而不是提取信息并自己做。

关于java - 不能从静态上下文中引用非静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14584215/

相关文章:

java - 如何将值传递给自定义注释?

java - 重构前置代码

c# - 如何从另一个页面调用 MainWindow 上的方法

c++ - boost::signals2 插槽作为非静态函数成员?

java - "Non-static variable this cannot be referenced from a static context"当我尝试实例化一个对象时 - 为什么?

java - equals()方法和==相等还是不相等?

java - TCP 扫描 : Unexpected Socket Exceptions

java - Java中的保留字

Java方法帮助

java - Java 中静态和非静态前向引用的内部工作