Java 具有不同方法参数的子类
Java subclass with different method parameters
我正在尝试制作模拟简单生物和食肉生物的模拟。
我有一个名为 creature 的 class 和一个名为 carnCreature 的子class。我在生物中有一个名为 eat 的方法,它接受一种类型的对象,但我需要 carnCreature class 中的 eat 方法来接受生物列表。我尝试将方法命名为与生物 class 中的命名相同,但是当我尝试调用它时,java 不接受更新的参数。
package simulationObjects;
import java.awt.Color;
import java.util.List;
import java.util.Random;
import java.lang.Math.*;
public class Creature {
public int x;
public int y;
public int maxTilesX;
public int maxTilesY;
public Color color;
public float health = 50;
public int life = 0;
public Creature (int x, int y, Color color, int maxTilesX, int maxTilesY) {
this.x = x;
this.y = y;
this.color = color;
this.maxTilesX = maxTilesX;
this.maxTilesY = maxTilesY;
}
public void update(Tile tile) {
eat(tile);
life++;
health-=1;
}
public void eat(Tile currentTile) {
if (currentTile.color == this.color) {
health += 3;
currentTile.color = Color.GRAY;
}
}
public boolean isCarnivore() {
return false;
}
}
package simulationObjects;
import java.awt.Color;
import java.util.List;
public class CarnCreature extends Creature{
private static final boolean CANABOLIC = false;
public CarnCreature(int x, int y, Color color, int maxTilesX, int maxTilesY) {
super(x, y, color, maxTilesX, maxTilesY);
// TODO Auto-generated constructor stub
}
public void update(List<Creature> creatures) {
eat(creatures);
life++;
health-=1;
}
public void eat(List<Creature> creatures) {
for (Creature creature : creatures) {
if (CANABOLIC) {
if (creature.color == this.color) {
health += 3;
creature.health = 0;
}
} else {
if (creature.color == this.color && creature.isCarnivore() == false) {
health += 3;
creature.health = 0;
}
}
}
}
public boolean isCarnivore() {
return true;
}
}
稍后调用 eat 函数是这样的:
for (Creature creature : creatures) {
if (creature.isCarnivore()) {
creature.upadte(creatures);
} else {
creature.update(tiles.get(creature.x).get(creature.y));
}
}
我试图将生物和 carnCreatures 存储在同一个列表中,"creatures."这是问题所在吗,我需要将它们存储在单独的列表中吗?
谢谢
isCarnivore()
测试不会让您在操作声明类型 Creature
基础 class :[=18 时强制转换为子 class 类型=]
for (Creature creature : creatures) {
if (creature.isCarnivore()) {
((CarnCreature)creature).update(creatures);
} else {
creature.update(tiles.get(creature.x).get(creature.y));
}
}
所以 isCarnivore()
显得无助,因为 if (instanceof CarnCreature)
会产生同样的效果和后果。
Is this the problem, and do I need to store them in separate lists?
最好不要以统一的方式操作它们。
使用基数 class 将它们分组到一个唯一的列表中会使您的任务更加困难。
但其实你有更深层次的问题。这里eat()
不是重写方法,而是subclass中的重载方法。 update()
也一样。
这意味着在这两种情况下,这两种方法都在 subclass.
中定义
这样的设计将不允许从多态性功能中受益,因为您想在基础 class 实例上调用第一个方法并在 subclass 实例上调用重载方法。
就概念而言,食肉动物不是生物。他们的行为类型非常不同:一个消耗一个东西(一块瓷砖),另一个消耗一个非常不同的东西(一个生物列表)。
要从多态性中获益,您应该重新设计基 class 和子 class 以覆盖方法而不是重载它们。但是,当您在参数中传递完全不同的类型时,您就会陷入困境。
因此,在您的情况下,我认为我什至不会在这些 classes 之间创建继承关系。
您有两个选择:
- 一旦你知道该生物是否是食肉动物,就施放它并访问方法
- 创建一个具有相同 "signature" 的方法,即相同的名称和参数。
第二个选项更优雅。使用 "magic" 的多态性,每个 class 都会调用其方法,您不需要使用 isCarnivore() 方法检查 class。但是您需要从图块中获取生物列表。
我正在尝试制作模拟简单生物和食肉生物的模拟。
我有一个名为 creature 的 class 和一个名为 carnCreature 的子class。我在生物中有一个名为 eat 的方法,它接受一种类型的对象,但我需要 carnCreature class 中的 eat 方法来接受生物列表。我尝试将方法命名为与生物 class 中的命名相同,但是当我尝试调用它时,java 不接受更新的参数。
package simulationObjects;
import java.awt.Color;
import java.util.List;
import java.util.Random;
import java.lang.Math.*;
public class Creature {
public int x;
public int y;
public int maxTilesX;
public int maxTilesY;
public Color color;
public float health = 50;
public int life = 0;
public Creature (int x, int y, Color color, int maxTilesX, int maxTilesY) {
this.x = x;
this.y = y;
this.color = color;
this.maxTilesX = maxTilesX;
this.maxTilesY = maxTilesY;
}
public void update(Tile tile) {
eat(tile);
life++;
health-=1;
}
public void eat(Tile currentTile) {
if (currentTile.color == this.color) {
health += 3;
currentTile.color = Color.GRAY;
}
}
public boolean isCarnivore() {
return false;
}
}
package simulationObjects;
import java.awt.Color;
import java.util.List;
public class CarnCreature extends Creature{
private static final boolean CANABOLIC = false;
public CarnCreature(int x, int y, Color color, int maxTilesX, int maxTilesY) {
super(x, y, color, maxTilesX, maxTilesY);
// TODO Auto-generated constructor stub
}
public void update(List<Creature> creatures) {
eat(creatures);
life++;
health-=1;
}
public void eat(List<Creature> creatures) {
for (Creature creature : creatures) {
if (CANABOLIC) {
if (creature.color == this.color) {
health += 3;
creature.health = 0;
}
} else {
if (creature.color == this.color && creature.isCarnivore() == false) {
health += 3;
creature.health = 0;
}
}
}
}
public boolean isCarnivore() {
return true;
}
}
稍后调用 eat 函数是这样的:
for (Creature creature : creatures) {
if (creature.isCarnivore()) {
creature.upadte(creatures);
} else {
creature.update(tiles.get(creature.x).get(creature.y));
}
}
我试图将生物和 carnCreatures 存储在同一个列表中,"creatures."这是问题所在吗,我需要将它们存储在单独的列表中吗?
谢谢
isCarnivore()
测试不会让您在操作声明类型 Creature
基础 class :[=18 时强制转换为子 class 类型=]
for (Creature creature : creatures) {
if (creature.isCarnivore()) {
((CarnCreature)creature).update(creatures);
} else {
creature.update(tiles.get(creature.x).get(creature.y));
}
}
所以 isCarnivore()
显得无助,因为 if (instanceof CarnCreature)
会产生同样的效果和后果。
Is this the problem, and do I need to store them in separate lists?
最好不要以统一的方式操作它们。
使用基数 class 将它们分组到一个唯一的列表中会使您的任务更加困难。
但其实你有更深层次的问题。这里eat()
不是重写方法,而是subclass中的重载方法。 update()
也一样。
这意味着在这两种情况下,这两种方法都在 subclass.
中定义
这样的设计将不允许从多态性功能中受益,因为您想在基础 class 实例上调用第一个方法并在 subclass 实例上调用重载方法。
就概念而言,食肉动物不是生物。他们的行为类型非常不同:一个消耗一个东西(一块瓷砖),另一个消耗一个非常不同的东西(一个生物列表)。
要从多态性中获益,您应该重新设计基 class 和子 class 以覆盖方法而不是重载它们。但是,当您在参数中传递完全不同的类型时,您就会陷入困境。
因此,在您的情况下,我认为我什至不会在这些 classes 之间创建继承关系。
您有两个选择:
- 一旦你知道该生物是否是食肉动物,就施放它并访问方法
- 创建一个具有相同 "signature" 的方法,即相同的名称和参数。
第二个选项更优雅。使用 "magic" 的多态性,每个 class 都会调用其方法,您不需要使用 isCarnivore() 方法检查 class。但是您需要从图块中获取生物列表。