简体   繁体   English

如何从列表访问子类对象的方法

[英]How to access a method of an subclass object from a List

I have a class Sprites and a class Soldier , Tank and so on. 我有一个Sprites类和一个SoldierTank等类。 Soldier and Tank extends Sprites . SoldierTank延伸Sprites Now the subclasses have a special update() method which is different in each class (like different movement and so on). 现在,子类具有一个特殊的update()方法,该方法在每个类中都不同(例如,不同的移动方式等)。

I have a List<Sprites> ListUnits1Player1 where all objects of eg Soldiers are stored and also a List<Sprites> ListUnits2Player1 where all objects of Tanks are stored. 我有一个List<Sprites> ListUnits1Player1 ,其中存储了士兵的所有对象,还有一个List<Sprites> ListUnits2Player1 ,其中存储了坦克的所有对象。 These lists are of the type <Sprites> . 这些列表的类型为<Sprites> Now please see the follwing code: 现在请查看以下代码:

        for(Iterator<Sprites> itr_unit1_player1 =     ListUnits1Player1.iterator();itr_unit1_player1.hasNext();) {
        Sprites unit1_player1 = itr_unit1_player1.next();
        unit1_player1.update();
    }

I get an error can't resolve method update() . 我收到一个无法解决方法update()的错误。

The debugger tells me however, that unit1_player1 is of type Soldier , which is correct since I stored the objects of Soldier in the List ListUnits1Player1 . 调试器告诉我,unit1_player1是Soldier类型的,这是正确的,因为我将Soldier的对象存储在List ListUnits1Player1

But then why can't I call the method update() of the class Soldier? 但是,为什么我不能调用士兵类的方法update()

I guess I could explicitly cast unit1_player1 to Soldier with ((Soldier)unit1_player1).update(); 我想我可以使用((Soldier)unit1_player1).update();unit1_player1显式unit1_player1Soldier ((Soldier)unit1_player1).update(); but since it is variable which unit (Soldier, Tank,...) is stored in ListUnits1Player1 I don't want to cast it to a certain type. 但是由于哪个单位(士兵,坦克等)存储在ListUnits1Player1是可变的, ListUnits1Player1我不想将其转换为某种类型。

I have tried casting with: 我尝试使用以下方法进行投射:

unitClass = unit1_player1.getClass();
((unitClass)unit1_player1).update(); 

to always get the correct class, but this just gives me the error Unknown class 'unitClass' 总是得到正确的类,但这只会给我错误Unknown class 'unitClass'

Any suggestions? 有什么建议么?

You could move the method update in the parent class Sprites as an abstract method. 您可以将父类Sprites的方法update作为抽象方法移动。

The subclasses then will have to override the method and unit1_player1.update(); 然后,子类将必须重写方法和unit1_player1.update(); would work without any cast. 没有任何演员就可以工作。

If you can not call unpdate on Sprites , means you did not declare update method in the super class or declare it as private. 如果您不能在Sprites上调用unpdate ,则意味着您没有在超类中声明update方法或未将其声明为私有方法。

It's always a good habit to use override annotation: 使用override注释始终是一个好习惯:

class Sprites {
    void update() {

    }
}

class Tank extends Sprites {

    @Override
    void update() {
        // ...
    }
}

Put your update method as abstract in Sprites class. 将您的update方法作为抽象放在Sprites类中。

//superclass
public abstract void update ();

Then override it in each subclass 然后在每个子类中覆盖它

//subclass
public void update (){
    //code
}

You declared an iterator over Sprite s, so from the compiler's point of view, you could be dealing with any subtype of that, but there's no way to know which 您在Sprite声明了一个迭代器,因此从编译器的角度来看,您可以处理该子集的任何子类型,但是无法知道哪个

If you will only ever have objects in your collection that support update , you should declare an interface for that method, make both Soldier and Tank implement it, and then debate your collection as containing objects of that interface type. 如果您的集合中只会有支持update对象,则应为该方法声明一个接口,让SoldierTank实现它,然后将您的集合辩论为包含该接口类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM