1.創建新class稱為Dog,Animal和Fish
2.創建新class稱為DogList 在src文件夾中
public
class
Fish extends
Animal{
}
public
class
Animal {
}
public
class
Dog extends
Animal{
}
public
class
DogList {
private
Dog[] theList
= new
Dog[5];
private
int
i=0;
public
void
add(Dog d){
if(i<theList.length){
theList[i]=d;
System.out.println("Dog
added at index "+i);
i++;
}
}
}
class
apples{
public
static
void
main(String[] args){
DogList
DLO = new
DogList();
Dog
d = new
Dog();
DLO.add(d);
}
}
這是運行時的結果:
Dog
added at index 0
沒有留言:
張貼留言