比较不同类别使用 instanceof

huangapple 未分类评论46阅读模式
英文:

Comparing different classes using instanceof

问题

  • Entity.class抽象母类
  • Player.class继承自Entity
  • Wall.class继承自Entity
                 public class slkjflksdjf
                 {
                     ArrayList entities = new ArrayList();
                     Player player = new Player();
                     Wall wall = new Wall();
    
                     public slkjflksdjf()
                     {
        
                         entities.add(player);
                         entities.add(wall);
        
                         for(int i = 0; i < entities.size(); i++)
                         {
            
                             if(entities.get(i) instanceof Wall)
                             {
                                 //进行某些操作
                             }
            
                         }
        
                     }
                 }

在检查完整的实体列表时,通过instanceof检查会反复触发“进行某些操作”的代码块,换句话说,会快速重复播放音频剪辑,哈哈...
所以从我所能看到的情况来看,它将Wall和Player都视为Entity,因此使用instanceof会导致它返回true,即使它们都是原始Entity的扩展?

英文:
  • Entity.class abstract mother class
  • Player.class extends Entity
  • Wall.class extends Entity
                 public class slkjflksdjf
                 {
                     ArrayList entities = new ArrayList();
                     Player player = new Player();
                     Wall wall = new Wall();
    
                     public slkjflksdjf()
                     {
        
                         entities.add(player);
                         entities.add(wall);
        
                         for(int i = 0; i &lt; entities.size(); i++)
                         {
            
                             if(entities.get(i) instanceof Wall)
                             {
                                 //do something
                             }
            
                         }
        
                     }
                 }

When checking instanceof through my actual full size list of entities it's throwing the "do this" block over and over rapidly, in other words playing an audio clip rapidly lol..
so ye from what i can tell it's treating the Wall and Player both as Entity so doing InstanceOf is just causing it to pull a true even if they are all extensions of the Original Entity?

答案1

得分: 0

如果玩家(Player)和墙(Wall)都扩展自实体(Entity)接口,则可以执行以下操作:

ArrayList<Entity> entities = new ArrayList<Entity>();
Player player = new Player();
Wall wall = new Wall();

entities.add(0, player);
entities.add(1, wall);

// entities.get(0) instanceof Player 为真
// entities.get(0) instanceof Wall 为假
// entities.get(0) instanceof Entity 为真

// entities.get(1) instanceof Player 为假
// entities.get(1) instanceof Wall 为真
// entities.get(1) instanceof Entity 为真

for(Entity entity : entities) {

  if(entity instanceof Player) {
    // 是玩家
  } else if(entity instanceof Wall) {
    // 是墙
  }

}

希望对你有所帮助。

英文:

If both Player and Wall extends Entity interaface you can do following:

ArrayList&lt;Entity&gt; entities = new ArrayList&lt;Entity&gt;();
Player player = new Player();
Wall wall = new Wall();

entities.add(0, player);
entities.add(1, wall);

// entities.get(0) instanceof Player is true
// entities.get(0) instanceof Wall is false
// entities.get(0) instanceof Entity is true

// entities.get(1) instanceof Player is false
// entities.get(1) instanceof Wall is true
// entities.get(1) instanceof Entity is true

for(Entity entity : entities) {

  if(entity instanceof Player) {
    // is player
  } else if(entity instanceof Wall) {
    // is wall
  }

}

hope this helps

答案2

得分: 0

如果Player是Entity的一个子类,而Wall是Entity的一个子类:

player instanceof Entity会返回true。

如果Wall是Entity的一个子类:

wall instanceof Entity会返回true。

然而反过来不成立(即entity instanceof Wall)。
为什么呢?因为这是instanceof操作符的定义方式。

此外,如果Wall和Player之间没有关联(意味着它们不是彼此的子类,或者在接口情况下没有实现关系)。
player instanceof Wall会返回false,
wall instanceof Player会返回false。

英文:

If Player is a subclass of Entity and Wall is a subclass of Entity:

player instanceof Entity returns true.

If Wall is a subclass of Entity:

wall instanceof Entity returns true.

However the reverse is not true (.i.e entity instanceof Wall)
Why? Because that is how instanceof operator is defined.

Moreover if Wall and Player are not related (meaning one is not subclass of the other or implementing in case of interfaces).
player instanceof Wall returns false and
wall instanceof Player returns false.

huangapple
  • 本文由 发表于 2020年4月6日 07:07:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61050694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定