什么是一个很好的替代模式......?
注意:每次我想要访问子类的方法时,我都希望避免向下转换对象,即 ((Bicycle)vehicles[1]).getSaddles ()
位。 Vehicle
的子类只会很简单,带有一个接受值和一些 getter 的构造函数,但 getter 并不相同。
public class Main {
Vehicle[] vehicles;
public static void main(String[] args) {
new Main();
}
private Main() {
DataStuff data = new DataStuff();
vehicles = new Vehicle[data.getNext()];
int i=0;
while(i<vehicles.length) {
int type = data.getNext();
if (type == 1) {
vehicles[i] = new Car(VehicleType.car, data);
} else if (type == 2) {
vehicles[i] = new Bicycle(VehicleType.bicycle, data);
}
i++;
}
if (vehicles[1].type.equals(VehicleType.bicycle)) {
System.out.println(vehicles[1].type.toString() + ", number of saddles:" + ((Bicycle)vehicles[1]).getSaddles());
}
}
public class DataStuff {
private int[] data = new int[] {2, 1, 4, 5, 1, 4, 2, 2, 1, 1};
private int pointer = 0;
public int getNext() {
return data[pointer++];
}
}
}
...
public class Vehicle {
public VehicleType type;
public Vehicle(VehicleType type) {
this.type = type;
}
}
...
public enum VehicleType {
car,
bicycle
}
...
public class Bicycle extends Vehicle {
private int wheels;
private int bells;
private int saddles;
public Bicycle(VehicleType type, DataStuff data) {
super(type);
wheels = data.getNext();
bells = data.getNext();
saddles = data.getNext();
}
public int getWheels() {
return wheels;
}
public int getBells() {
return bells;
}
public int getSaddles() {
return saddles;
}
}
...
public class Car extends Vehicle {
private int wheels;
private int seats;
private int engines;
private int doors;
public Car(VehicleType type, DataStuff data) {
super(type);
wheels = data.getNext();
seats = data.getNext();
engines = data.getNext();
doors = data.getNext();
}
public int getWheels() {
return wheels;
}
public int getSeats() {
return seats;
}
public int getEngines() {
return engines;
}
public int getDoors() {
return doors;
}
}
更新1
假设对象 ObjectA 和 ObjectB 完全不同,但我必须以通用方式存储它们。 ObjectA 可以是一个国家的定义,ObjectB 可以是一棵树...
更新2
(我还更新了上面的伪代码以修复错误并使其更加明显)我希望不要编写太多代码,因为我不希望人们陷入我的测试用例的具体细节中,但是上面的代码接近我需要实现的目标。
我的问题可能措辞不好。我会尽量不那么含糊。
所以...我正在以随机顺序读取有关任意对象的数据,这些对象确实共享属性(尽管我上面的代码没有反射(reflect)这一点)。我需要能够在解析 int[] 后迭代对象并获取每个对象的特定数据...但是,我想避免我必须做的所有向下转换( 中的示例) println
语句)。
什么是好的替代模式?或者这样就可以了吗?我不必有巨大的 if else block ,因为 int[] 中的数据(不用担心 DataStuff 类,这只是一个简单的示例)将全部基于查询生成(上面也未显示)代码)将由实现提供,因此开发人员将知道他期望返回什么(开发人员我指的是我,哈哈...)。
我希望这足以描述我想要实现的目标。
最佳答案
在不知道您的对象模型或您的对象正在做什么工作的情况下,我建议您只需在基类上添加一个抽象方法并覆盖该方法以执行适当的操作。这是基本的多态行为。
abstract class ChessPiece {
abstract void move(Board board);
}
class Knight extends ChessPiece {
@Override
public void move(Board board) {
//...weeee, I'm a Knight
}
}
class Bishop extends ChessPiece {
@Override
public void move(Board board) {
//...weeee, I'm a Bishop
}
}
如果您发现自己在进行转换并使用 instanceof
,这通常表明您做错了。
更新:
在这种情况下仅打印鞍座详细信息可能需要向下转换,但为什么不在每个 Vehicle
上覆盖一个 toString
方法并打印出 自行车
对象?
if (vehicles[1].type.equals(VehicleType.bicycle)) {
System.out.println(vehicles[1]);
}
public class Bicycle extends Vehicle {
@Override
public String toString() {
return this.saddles;
}
}
如果这还不够,我想您可以在 Bicycle 上编写一个静态方法,该方法接受 Bicycle 并打印出其鞍座信息,然后只需在 print 语句中调用该方法,如下所示:
System.out.println(vehicles[1].type.toString() + ", number of saddles:" + Bicycle.getSaddleInfo(Bicycle.class.cast(vehicles[1])));
关于java - 比沮丧更好的选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20363503/