특정 클래스의 모든 이름을 나열하는 메서드를 작성하는 동안이 오류에 문제가 있습니다. (하단의 오류) 나는 몇 가지 시도했지만 내 인생에 대해, 그것을 알아낼 수 없습니다. 도와주세요, 고마워요."정적이 아닌 메서드를 정적 컨텍스트에서 참조 할 수 없습니다"오류가 발생했습니다.
클래스 고양이 :
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
}
}
}
이 도와주세요, 감사
감사합니다. 나는 또 다른 질문이있다. public void indexDisplay가 작동하지 않습니다. 내 다른 클래스의 이름을 출력하고 대신 설정 한 매개 변수를 인쇄합니다. 나는 '인덱스'를 평등하게 만들어야한다고 생각하지만, 이것 역시 문제가 있습니다. 어떤 제안? –
@JoshuaBaker : 다른 질문이있는 경우 다른 질문으로 질문해야합니다. 이상적으로는 문제를 보여주는 짧고 완벽한 프로그램이 이상적입니다. –