HappyActor actor1= new HappyActor();
SadActor  actor2= new SadActor();
  
ArrayList actors= new ArrayList();
actors.add(actor1);
actors.add(actor2);Actor actor = (Actor)actors.get(0);
actor.act(); // print out "I am HappyActor"
actor = (Actor)actors.get(1);
actor.act(); // print out "I am SadActor"或者
for (Iterator it = actors.Iterator(); it.hasNext(); )
{
   Object obj = it.next();
   if (obj instanceof HappyActor) {
      ((HappyActor)obj).act(); // invoke the act() in HappyActor
   } 
   if (obj instanceof SadActor) {
      ((SadActor)obj).act(); // invoke the act() in SadActor
   } 
}

解决方案 »

  1.   

    用“instanceof”可以解决这个问题 (动态type识别)另外,你也可以直接用(Actor)obj.act(),这是override的典型用法,
    这时, 可以保证HappyActor或SadActor中的instance方法act()将被
    “动态绑定”。可以这样理解,Actor是一个interface,其中的act()方法是一个抽象
    方法,根本就没有被实现,所以只能是HappyActor或SadActor中的
    act()被调用。如果act()是一个抽象类中的抽象方法,也是这样。如果act()是一个父类中的非静态非抽象方法,也是这样。如果act()是一个父类中的静态非抽象方法,则不是这样,这时
    不是override,而是hide。这时父类中的act()实现将被调用
      

  2.   

    现在是不是很少用Vector了,改用ArrayList了?
    但是我看了一下,他们的功能基本差不多. ArrayList好在什么地方啊
      

  3.   

    主要是在同步上:  from the html-doc of ArrayList:
    This class is roughly equivalent to Vector, except that it is unsynchronized.
    Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:  List list = Collections.synchronizedList(new ArrayList(...));
     
      

  4.   

    补充:当不需要同步时,ArrayList要比Vector速度快
      

  5.   

    hahaha88(忧郁的眼神,稀嘘的胡子喳)看到动态帮定这个词想起来了,哈哈.也就是说在继承类里面被override的act()方法会被
    自动帮定到对应的方法下面咯.那么Vector类里面会丢失type information应该不是问题.我只要把从里面拿出来的句柄upcast成我的超类就可以了,其余的事情就由compiler来解决.能不能这样理解?
      

  6.   

    我也是这样理解的,再补充一点:如果你仅仅是想要调用不同的act()的话,那么在你的
    code里,你就不必关心到底从Vector里取出来的是HappyActor还是SadActor,你一律
    用Actor来操作他们就可以了,override机制会自动处理剩下的具体问题。如果因为某种原因,你确实需要知道从Vector里取出来的Object的“runtime type”,
    那就可以用instanceof来判断
      

  7.   

    把取出的object造型到Actor是最好的,因为这正是面向对象的精神所在!
      

  8.   

    还是我,哈哈.那么如果我在继承Actor那个interface以后,在Sad-以及HappyActor里面分别又定义了
    各自不同的方法(act()方法保留),当我把从向量里取出的reference上溯成actor后,我将会
    丢失在interface里面没有定义的方法,是吗?傻问题,哈哈.
      

  9.   

    如果照simoncn(Simon)说的第二种方法,我想不会的。
      

  10.   

    kf_young(野风)说的对,这时,实际上,你的HappyActor和SadActor
    已经和Actor无关了,所以就不能用Actor来”抽象“他们了,这时
    就应该用instanceof 来区别他们了
      

  11.   

    以下是Test.java 可运行,请笑纳。
    java 中的所有类方法都是virtrue 的,是什么类就实际调用方法,而不受声名影响。import java.util.Vector;interface Actor{  
        void act();
    }class HappyActor implements Actor {
        public void act(){
            System.out.println("I am HappyActor");
        }
    }class SadActor implements Actor {
        public void act(){
            System.out.println("I am SadActor");
        }
    }//** Some other Code here
    public class Test {
        public static void main(String[] args) {
        //** Some code here        HappyActor actor1= new HappyActor();
            SadActor  actor2= new SadActor();
      
            Vector actors= new Vector();
        
            actors.add(actor1);
            actors.add(actor2);
        
            for (int i = 0; i<actors.size();i++){
                Actor tmpActor = (Actor) actors.elementAt(i);
                tmpActor.act();
            }
        }
    }