问题与“非静态方法不能从静态上下文引用”错误
问题描述:
我遇到此错误的问题,同时试图编写一个方法,列出特定类中的所有名称。 (在底部的错误)我已经尝试了一些事情,但对于我的生活,无法弄清楚。请帮忙,谢谢。问题与“非静态方法不能从静态上下文引用”错误
类猫:
public class Cat
{
// instance variables
private String name;
private int yearOfBirth;
private int weightInKilos;
public Cat() {
setName("");
setYearOfBirth(0);
setWeightInKilos(0);
}
/**
*
*/
public Cat(String newName, int newYearOfBirth, int newWieghtInKilos)
{
setName(newName);
setYearOfBirth(newYearOfBirth);
setWeightInKilos(newWieghtInKilos);
}
public String getName(){
return name;
}
public int getYearOfBirth(){
return yearOfBirth;
}
public int getWieghtInKilos(){
return weightInKilos;
}
public void setName(String newName){
if (newName != null){
name = newName;
}
else{
System.out.println("Invalid Name");
}
}
public void setYearOfBirth(int newYearOfBirth){
if (yearOfBirth >= 0){
yearOfBirth = newYearOfBirth;
}
else{
System.out.println("Year Of Birth must not be negative!");
}
}
public void setWeightInKilos(int newWeightInKilos){
if (weightInKilos >= 0){
weightInKilos = newWeightInKilos;
}
else{
System.out.println("Weight must not be negative!");
}
}
}
类猫舍:
import java.util.ArrayList;
public class Cattery
{
// instance variables - replace the example below with your own
private ArrayList <Cat> cats;
private String businessName;
/**
* Constructor for objects of class Cattery
*/
public Cattery(String NewBusinessName)
{
cats = new ArrayList <Cat>();
NewBusinessName = businessName;
}
public void addCat(Cat newCat){
cats.add(newCat);
}
public void indexDisplay(int index) {
if((index >= 0) && (index <= cats.size()-1)) {
System.out.println(index);
}
else{
System.out.println("Invalid index position!");
}
}
public void removeCat(int indexremove){
if((indexremove >= 0) && (indexremove <= cats.size()-1)) {
cats.remove(indexremove);
}
else{
System.out.println("Invalid index position!");
}
}
public void displayNames(){
System.out.println("The current guests in Puss in Boots Cattery:");
for(Cat catNames : cats){
System.out.println(Cat.getName()); //ERROR; non static method cannot be referenced from a static context..wtf
}
}
}
请帮帮忙,谢谢
答
用途:
System.out.println(catNames.getName());
getName
是非静态函数,所以哟你需要在该类的一个实例上使用它,就像在cats
列表中那样。
答
如果您有实例方法,则需要在该类的特定实例上将其称为。
这里:
System.out.println(Cat.getName());
你想调用它本身Cat
类。你想:
for (Cat cat : cats) {
System.out.println(cat.getName());
}
注意,我从catNames
改变迭代变量的名称cat
以及 - 因为该值只为“我们正在寻找的那一刻猫”的引用。这不是猫名称,也不是多只猫(或猫的名字) - 它是一只猫。仔细命名变量非常重要 - 它可以帮助正确的代码看起来正确,并且不正确的代码到看起来不正确。将getName()
称为catNames
...(名称集合的名称是什么?),但绝对是对称为cat
的变量有意义。
您原始代码的另一个警告响铃是您的for
循环的主体没有使用迭代变量 - 这几乎总是暗示某些事情是错误的。固定版本当然会。
答
for (Cat cat : cats) {
System.out.println(Cat.getName());
}
在这里你需要使用猫,而不是猫。因此,使用
for (Cat cat : cats) {
System.out.println(cat.getName());
}
答
Cat.getName() this line means getName() should be static method in Cat class, but's not as such.
通过instacne所以访问getName()
方法。
System.out.println(catNames.getName());
谢谢你的工作,现在我明白了。我有另一个问题。我的公共无效indexDisplay不起作用。我希望它打印出我的其他类的名称,而不是打印出我设置的参数。我认为我需要让'索引'等于某件事,但我也遇到了麻烦。有什么建议么? – 2013-03-04 08:40:58
@JoshuaBaker:如果你有另一个问题,你应该问*作为另一个问题 - 理想情况下用一个简短但完整的程序*只显示问题。 – 2013-03-04 08:41:49