简体   繁体   English

当传递的参数是不同对象的数组时,如何在方法中声明参数类型?

[英]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 . 一种惯用的方法是为GroundEnemyFlyingEnemy创建一个接口 (或抽象类)。

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. 举例来说,如果两个FlyingEnemyGroundEnemy有类似性质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并指定与GroudEnemyFlyingEnemy相关的方法。 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 . 现在, GroudEnemyFlyingEnemy都有方法attack

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

相关问题 使用Mockito,如何使用返回类型void存储一个方法,该方法在传递某个参数时会抛出异常? - With Mockito, how to stub a method with return type void which throws an exception when a certain argument is passed? arguments 类型是否与参数一起传递给方法? - Are type arguments passed along with an argument to a method? 调用具有不同参数类型的方法 - Invoking method with different argument type 方法定义作为参数传递 - Method definition passed as argument 如何将方法的返回类型声明为传递给方法的数组中最后一个lambda的返回类型 - How to declare method's return type the as return type of last lambda in array passed to the method 如何捕获或验证传递给私有方法的参数? - How to capture or verify the argument passed to a private method? 有一个方法接受不同的对象作为参数 - Having a method accept different objects as an argument 我可以使用固定大小的数组参数声明 Java 方法吗? - Can I declare a Java method with a fixed-sized array argument? 作为参数传递时如何模拟依赖项? - How to mock an dependency when passed as an argument? 在同一方法中作为参数传递的不同自定义类型的两个列表 - Two lists of different custom types to be passed in the same method as an argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM