1 import java.util.*; 2 3 public class SixDuo { 4 public void eat(){ 5 System.out.println("狗狗吃骨头"); 6 } 7 public static void main(String[] args){ 8 ArrayList<Object> myAL = new ArrayList<Object>(); 9 SixDuo Dogc = new SixDuo(); 10 Object obj = new Object(); 11 myAL.add(Dogc); //加入myAL 12 myAL.add(obj); 13 Object o = myAL.get(0); //取出在myAL里排第一位的Dogc 14 SixDuo d1 = (SixDuo)o; //将类型转换成SixDuo 15 d1.eat(); 16 if(o instanceof SixDuo){ //为了防止o取出的不一定是SixDuo类型而进行判断 17 SixDuo d2 = (SixDuo) o; 18 d2.eat(); 19 } 20 } 21 22 }