当使用继承时,你可以创建两个类A
和B
,它们继承自类C
。然后你可以创建一个C
数组来存储这两个类中的任何一个--C[]
。
但是,当使用组合时,数组必须是什么类型才能存储这两种类型中的任何一种?
class Entity {
public int id;
public Entity() {
this.id = 0;
}
}
class Player extends Entity {
public Player() {
this.id = 1;
}
}
class Monster extends Entity {
public Monster() {
this.id = 2;
}
}
public class Main {
public static void main(String[] args) {
Entity[] entities = new Entity[2];
entities[0] = new Player(); // id == 1
entities[1] = new Monster(); // id == 2
}
}
字符串
当使用复合时,你必须将Entity存储为字段:
class Entity {
public int id;
public Entity() {
this.id = 0;
}
}
class Player {
Entity entity;
public Player() {
this.entity = new Entity();
this.entity.id = 1;
}
}
class Monster {
Entity entity;
public Monster() {
this.entity = new Entity();
this.entity.id = 2;
}
}
public class Main {
public static void main(String[] args) {
Player player = new Player();
Monster monster = new Monster();
Entity[] entities = new Entity[2];
// TODO: won't work! what type?
entities[0] = player;
entities[1] = monster;
}
}
型
5条答案
按热度按时间kd3sttzy1#
你只能在这里存储实体。所以只需要添加player.entity/monster.entity或其他什么。
8fsztsew2#
当使用组合时,通常必须直接存储特定类类型的对象,而不是公共基类的数组。在您的示例中,您有由Entity对象组成的Player和Monster类,但您不能直接将它们存储在Entity数组中,因为Player和Monster不继承Entity。
字符串
jtjikinw3#
在你的例子中,你使用了强制转换。强制转换只适用于“is a”或“inheritance”关系。并不总是推荐使用组合。“favorite composition over inheritance”语句意味着不要在任何情况下都过度使用继承。但是当你想对一个像你的例子这样的真实的“is a”关系使用多态性这样的继承特性时,毫无疑问,不要后悔地使用它。错误继承关系的一个例子是这样的:
Animal“是一个”MoveableEntity => Animal“具有”MovingBehavior
sqougxex4#
如果C的接口足以满足您对这些项所需的所有操作,请使用C[]。
如果你只想存储A或B,不想存储其他任何东西,并且对它们进行不同的处理,而不是通过它们自己的方法,那么使用某种复合类型(例如OneOf<T1,T2>)是有意义的。这种类型包含A和B的字段,并且只使用其中一个,并且通常有(在其他方法中)方法:
字符串
其允许穷举处理包含在该复合中的所有可能类型。
92vpleto5#
答案已经作为问题的注解给出了;您需要使用一个公共超类,在本例中是Object。