2013-03-04 2 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클래스 자체에 호출을 시도하고 있습니다. 당신이 원하는 : 나뿐만 아니라 catcatNames에서 반복 변수의 이름을 변경 한

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

주 - 값이 "우리가 지금보고있는 고양이"에 불과 참조이기 때문이다. 고양이 의 이름이이 아니며 여러 고양이 (또는 고양이 이름)도 아닙니다. 이것은 단일 고양이입니다. 변수 이름을 신중하게 지정하는 것이 중요합니다. 코드를 으로 수정하면으로 표시되고 으로 코드가 잘못 표시되면으로 잘못 표시 될 수 있습니다. 을 catNames ... (이름 컬렉션의 이름은 무엇입니까?)라는 변수에 호출하는 것은 의미가 없지만 은 이라는 변수에서 호출해야합니다. 은 절대적으로입니다.

원본 코드의 또 다른 경고 벨은 for 루프 본문에 반복 변수가 사용되지 않았기 때문에 거의 항상 잘못된 것이 있음을 나타냅니다. 물론 고정 버전이 있습니다.

+0

감사합니다. 나는 또 다른 질문이있다. public void indexDisplay가 작동하지 않습니다. 내 다른 클래스의 이름을 출력하고 대신 설정 한 매개 변수를 인쇄합니다. 나는 '인덱스'를 평등하게 만들어야한다고 생각하지만, 이것 역시 문제가 있습니다. 어떤 제안? –

+0

@JoshuaBaker : 다른 질문이있는 경우 다른 질문으로 질문해야합니다. 이상적으로는 문제를 보여주는 짧고 완벽한 프로그램이 이상적입니다. –

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

여기서 Cat이 아닌 cat을 사용해야합니다. 그래서

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()); 
관련 문제