我做了一類叫做魚:使對象的數組
public class Fish {
private String species;
private int size;
//Constructor
public Fish(int x, String s) {
size = x;
species = s;
}
public String getSpecies() { return species; }
public int getSize() { return size; }
public String toString() {
return String.format("A %dcm %s", size, species);
}
}
的,我也開始做一類叫做池塘,是爲了有保存的數組稱爲「魚」的屬性魚對象。我不確定如何做到這一點。這是我迄今的嘗試。我
public class Pond {
private int capacity;
private Object[] fish; //This is what I am trying to initialize. list of Fish.
private int numFish;
//Capacity Constructor
public Pond(int n, int c) {
n = numFish;
c = capacity;
}
public int getNumFish() { return numFish; }
public boolean isFull() {
boolean isFull = false;
if (numFish >= capacity) {
isFull = true;
}
else {
isFull = false;
}
return isFull;
}
public String toString() {
return String.format("Pond with %d fish", numFish);
}
public void add(Fish aFish) {
if (numFish <= capacity) {
numFish += 1;
fish.add Fish;
}
else {
numFish += 0;
}
}
}
http://docs.oracle.com/javase/tutorial/爪哇/ nutsandbolts/arrays.html。順便說一句,我看不出任何問題。只是兩個注意事項:如果它是一個魚類數組,它應該是'Fish []'類型,並且被命名爲'fishes'(array ==> multiple fishes ==> plral form) –
@JBNizet魚也是魚的複數,我想(這裏不是母語)。但是,在這種情況下,魚類可能會更好。 –
@ peter.petrov:謝謝。我今天學到了東西。 –