2012年6月1日 星期五

Java Basic(59)-Class to Hold Objects


1.創建新class稱為DogAnimalFish



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

沒有留言:

張貼留言