두 클래스가 있습니다. 고양이 이름, 출생 연도 및 체중을 킬로로 유지하는 Cat이라는 클래스. 배열 인 Cattery라는 클래스가 있습니다. Cat 클래스의 고양이를 배열에 입력하고 싶습니다. 각 고양이는 Kilos에서 자체 이름, 출생 연도 및 체중을 갖습니다. 어떻게해야합니까? 고맙습니다.메서드를 개체 배열로 어떻게 호출합니까?
public class Cat {
private String name;
private int birthYear;
private double weightInKilos;
/**
* default constructor
*/
public Cat() {
}
/**
* @param name
* @param birthYear
* @param weightInKilos
*/
public Cat(String name, int birthYear, double weightInKilos){
this.name = name;
this.birthYear = birthYear;
this.weightInKilos = weightInKilo
}
/**
* @return the name.
*/
public String getName() {
return name;
}
/**
* @return the birthYear.
*/
public int getBirthYear() {
return birthYear;
}
/**
* @return the weightInKilos.
*/
public double getWeightInKilos() {
return weightInKilos;
}
/**
* @param the name variable.
*/
public void setName(String newName) {
name = newName;
}
/**
* @param the birthYear variable.
*/
public void setBirthYear(int newBirthYear) {
birthYear = newBirthYear;
}
/**
* @param the weightInKilos variable.
*/
public void setWeightInKilos(double newWeightInKilos) {
weightInKilos = newWeightInKilos;
}
}
배열 클래스.
import java.util.ArrayList;
public class Cattery {
private ArrayList<Cat> cats;
private String businessName;
/**
* @param Cattery for the Cattery field.
*/
public Cattery() {
cats = new ArrayList<Cat>();
this.businessName = businessName;
}
/**
* Add a cat to the cattery.
* @param catName the cat to be added.
*/
public void addCat(Cat name)
{
Cat.add(getName());
}
/**
* @return the number of cats.
*/
public int getNumberOfCats()
{
return cats.size();
}
}
음식 깡통을 두드리는 것이 더 효과적 일지 모르지만 "여기 키티"라고 해보십시오. –
'cats.add (새 고양이 (...))'를 호출 하시겠습니까? –