2013-03-04 70 views
0

我遇到此錯誤的問題,同時試圖編寫一個方法,列出特定類中的所有名稱。 (在底部的錯誤)我已經嘗試了一些事情,但對於我的生活,無法弄清楚。請幫忙,謝謝。問題與「非靜態方法不能從靜態上下文引用」錯誤

類貓:

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 

} 
} 
} 

請幫幫忙,謝謝

回答

2

用途:

System.out.println(catNames.getName()); 

getName是非靜態函數,所以喲你需要在該類的一個實例上使用它,就像在cats列表中那樣。

1

如果您有實例方法,則需要在該類的特定實例上將其稱爲

這裏:

System.out.println(Cat.getName()); 

你想調用它本身Cat類。你想:

for (Cat cat : cats) { 
    System.out.println(cat.getName()); 
} 

注意,我從catNames改變迭代變量的名稱cat以及 - 因爲該值只爲「我們正在尋找的那一刻貓」的引用。這不是貓名稱,也不是多隻貓(或貓的名字) - 它是一隻貓。仔細命名變量非常重要 - 它可以幫助正確的代碼看起來正確,並且不正確的代碼到看起來不正確。將getName()稱爲catNames ...(名稱集合的名稱是什麼?),但絕對是對稱爲cat的變量有意義。

您原始代碼的另一個警告響鈴是您的for循環的主體沒有使用迭代變量 - 這幾乎總是暗示某些事情是錯誤的。固定版本當然會。

+0

謝謝你的工作,現在我明白了。我有另一個問題。我的公共無效indexDisplay不起作用。我希望它打印出我的其他類的名稱,而不是打印出我設置的參數。我認爲我需要讓'索引'等於某件事,但我也遇到了麻煩。有什麼建議麼? – 2013-03-04 08:40:58

+0

@JoshuaBaker:如果你有另一個問題,你應該問*作爲另一個問題 - 理想情況下用一個簡短但完整的程序*只顯示問題。 – 2013-03-04 08:41:49

0
for (Cat cat : cats) { 
    System.out.println(Cat.getName()); 
} 

在這裏你需要使用貓,而不是貓。因此,使用

for (Cat cat : cats) { 
    System.out.println(cat.getName()); 
} 
0
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()); 
相關問題