[英]How to declare argument type in method when argument passed is an array of different objects?
So I have two arrays, 所以我有两个数组
Array<GroundEnemy> groundEnemies;
Array<FlyingEnemy> flyingEnemies;
Then I have the methods to render enemies as, 然后我有方法将敌人渲染为
renderGroundEnemy(groundEnemies, delta);
renderFlyingEnemy(flyingEnemies, delta);
and I declared these methods as, 我将这些方法声明为
private void renderGroundEnemy(GroundEnemy enemies, delta){ ... }
private void renderFlyingEnemy(FlyingEnemy enemies, delta){ ... }
Because the method for rendering flying enemies is the same for rendering ground enemies I thought I would just reuse the same method. 因为渲染飞行敌人的方法与渲染地面敌人的方法相同,所以我想我会重复使用相同的方法。 Now I'm confused how to set the argument type for the method.
现在我很困惑如何为方法设置参数类型。 How do I set the argument type for the render method?
如何设置render方法的参数类型? I was thinking something like this but I still don't quite get it,
我当时在想这样的事情,但我还是不太明白,
private void renderEnemy(ArrayOfObjects enemies, delta){ ... }
One idiomatic approach would be to create an interface (or an abstract class) for both GroundEnemy
and FlyingEnemy
. 一种惯用的方法是为
GroundEnemy
和FlyingEnemy
创建一个接口 (或抽象类)。
For instance: 例如:
public interface Enemy { ... }
public class GroundEnemy implements Enemy { ... }
public class FlyingEnemy implements Enemy { ... }
Then you would be able to just use Enemy
: 然后您就可以使用
Enemy
:
private void renderEnemy(Array<? extends Enemy> enemies, delta) { ... }
The choice of interface vs abstract class is important: interface basically says: 接口与抽象类的选择很重要:接口基本上说:
"I have those two things: FlyingEnemy and PublicEnemy, and I want to interact with them in the very same way."
“我有两件事:FlyingEnemy和PublicEnemy,我想以同样的方式与他们互动。”
Whereas extending an abstract class means a bit more: 扩展抽象类意味着更多:
"I have those two things: FlyingEnemy and PublicEnemy, and I want to interact with them in the very same way. They also work quite similarly."
“我有两件事:FlyingEnemy和PublicEnemy,我想以相同的方式与他们互动。它们的工作方式也非常相似。”
For instance, if both FlyingEnemy
and GroundEnemy
have properties like stamina
, or killed
, or methods like .hit()
or .heal()
- then it might make sense to put all those into an abstract class and not repeat the implementation (the same implementation!) for both enemy types. 举例来说,如果两个
FlyingEnemy
和GroundEnemy
有类似性质stamina
,或killed
,或类似的方法.hit()
或.heal()
-那么它可能是有意义的把所有这些成一个抽象类,而不是重复执行(同实施!)。
See this StackOverflow question and following answers for a pretty good explanation of differences between the concept of an interface and of an abstract class. 请参见此StackOverflow问题和以下答案 ,以很好地解释接口概念与抽象类之间的区别。
One of the advantages of object oriented programming is you can do EXACTLY this. 面向对象编程的优点之一就是您可以做到这一点。 I'm assuming GroundEmenies and FlyingEnemies have more in common than just the rendering process.
我假设GroundEmenies和FlyingEnemies有更多的共同点,而不仅仅是渲染过程。 What convention tells you to do here is create a super class, Enemy, that holds the similarities between Flying and Ground enemies.
惯例告诉您在这里做的是创建一个超级类,敌人,该类拥有飞行和地面敌人之间的相似之处。
Then flying and ground enemies can extend the enemy class. 然后,飞行和地面敌人可以扩大敌人的等级。
public class GroundEnemy extends Enemy { ... }
public class FlyingEnemy extends Enemy { ... }
Now, any method that works with properties shared by both types of enemies can use Enemy as a parameter type. 现在,任何能与两种类型的敌人共享的属性一起使用的方法都可以将“敌人”用作参数类型。 Example:
例:
private void renderEnemy(Array<? extends Enemy> enemies, delta) { ... }
The best approach is to create super interface Enemy
and specify methods related to both GroudEnemy
and FlyingEnemy
. 最好的方法是创建超级接口
Enemy
并指定与GroudEnemy
和FlyingEnemy
相关的方法。 The code: 编码:
public interface Enemy {
public void attack(int damage);
}
public interface GroundEnemy extends Enemy { ... }
public interface FlyingEnemy extends Enemy { ... }
Now both GroudEnemy
and FlyingEnemy
has method attack
. 现在,
GroudEnemy
和FlyingEnemy
都有方法attack
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.