protected LivingBeing search(LivingBeing Target,int distance)
    {
        setEnergy(getEnergy() - 1);
        if(isDead())
            return null;
        Vector<LivingBeing> neighbors = simulation.getNeighbors(getRow(), getColumn(), distance);
        for(int neighborIndex = 0; neighborIndex < neighbors.size(); neighborIndex++)
            if(neighbors.get(neighborIndex) instanceof Target)
                return (AlgaeColony)neighbors.get(neighborIndex);        return null;
    }比如这样一个代码.
LivingBeing是一个超类,Target可以是LivingBeing的任何一次子类,就是说我并不知道LivingBeing具体从属于哪一个子类.
这段代码的目的是把neighbors中和Target从属于同一个子类的项取出.这里instanceof是错的,我知道.
instanceof前面如果是实例的话,后面必须是类名.但是我不知道Target属于那个子类,如果使用超类的类名LivingBeing,显然整个动态数组都会被取出.这样这段代码的功能就消失了.- - 很纠结.目前想个一个笨办法是先穷举判断Target属于哪个子类,然后再instanceof.那样的话把这个写到超类里就没有意义了,还不如一个子类写一个.

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【huajianjiu】截止到2008-07-05 11:55:25的历史汇总数据(不包括此帖):
    发帖的总数量:0                        发帖的总分数:0                        
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:---------------------结分的百分比:---------------------
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    if (name instanceof type)
    {

    }
      

  3.   

    Target.getClass()就行了,给你个例子看
    package craky.cmd.temp;public class Test
    {
        public static void main(String[] args)
        {
            A a = new A();
            A b = new B();
            A c = new C();
            A d = new B();
            
            System.out.println(a.getClass().getName()); //结果:craky.cmd.temp.A
            System.out.println(b.getClass().getName()); //结果:craky.cmd.temp.B
            System.out.println(c.getClass().getName()); //结果:craky.cmd.temp.C
            System.out.println(d.getClass().getName()); //结果:craky.cmd.temp.B
            System.out.println(d.getClass() == b.getClass()); //结果:true
        }
    }
    class A
    {}class B extends A
    {}class C extends A
    {}
      

  4.   

    isInstancepublic boolean isInstance(Object obj)    Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.    Specifically, if this Class object represents a declared class, this method returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses); it returns false otherwise. If this Class object represents an array class, this method returns true if the specified Object argument can be converted to an object of the array class by an identity conversion or by a widening reference conversion; it returns false otherwise. If this Class object represents an interface, this method returns true if the class or any superclass of the specified Object argument implements this interface; it returns false otherwise. If this Class object represents a primitive type, this method returns false.    Parameters:
            obj - the object to check 
        Returns:
            true if obj is an instance of this class
        Since:
            JDK1.1