我的Canvas
有几个Shape
个对象。这些对象是可选择的,并在lastSelection
和currentSelection
中进行跟踪。
在转移+点击时,我想通过一行连接一些Shapes
。但对于每个Shape
实现,连接的方式应该不同。
如何重写以下内容并删除1)instanceof检查,2)类型转换?
class Shape;
class Ellipse extends Shape;
class Line extends Shape;
class Square extends Shape;
//some more
Shape lastSelection;
Shape currentSelection;
onClick() {
if (lastSelection instanceof Ellipse && currentSelection instanceof Ellipse) {
connect((Ellipse) lastSelection, (Ellipse) currentSelection);
}
if (lastSelection instanceof Square && currentSelection instanceof Square) {
connect((Square) lastSelection, (Square) currentSelection);
}
//some more
}
connect(Ellipse e1, Ellipse 2) {}
connect(Square s1, Square s2) {}