找到要调用的子类

时间:2015-12-10 00:17:54

标签: java

我目前正在研究更多有关OOP概念的内容。我正在编写的程序是通过鼠标单击然后拖动到所需位置并释放鼠标来在屏幕上绘制形状。那么用户可以通过组合框选择来选择要绘制的形状。此组合框由Shape类型的项目组成。 Shape是多个形状对象的抽象类。 shape类有两个方法,draw(graphics)和getColor()。释放鼠标时,我需要知道正在使用哪个形状对象来绘制正确的形状。我可以使用instanceof来检查它可能是哪种形状,但我觉得这是一种不好的做法或设计气味。我还考虑在shape类中创建两个非抽象方法来设置从鼠标释放和鼠标单击的点。与形状相关的所有类都不是用于学习目的的Java API。你会采取什么方法?为什么?

public class ShapeMakerPanel extends JPanel
{
    private JPanel controls;

    private JPanel currentColor;

    private JComboBox<Shape> shapeChoice;

    private JCheckBox filled;

    private JButton undo;

    private JButton clear;

    private List<Shape> list = new ArrayList<Shape>();

    public ShapeMakerPanel() {
      controls = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
      currentColor = new JPanel();
      shapeChoice = new JComboBox<Shape>();
      undo = new JButton("Undo");
      clear = new JButton("Clear");
      filled = new JCheckBox("Filled");

      //Anything below this line in the constructor is being moved to a
      //createUI method, so please ignore this.
      controls.setName("controls");
      currentColor.setName("currentColor");
      undo.setName("undo");
      clear.setName("clear");
      shapeChoice.setName("shapeChoice");

      currentColor.setBackground(Color.BLACK);
      currentColor.setPreferredSize(new Dimension(25, 25));

      shapeChoice.addItem(new Rectangle());
      shapeChoice.addItem(new Oval());

      controls.add(shapeChoice);
      controls.add(currentColor);
      controls.add(filled);
      controls.add(undo);
      controls.add(clear);

      controls.addMouseListener(new ControlsPanelMouseListener());

      add(controls);
    }

    public List<Shape> getShapes () {
      return list;
    }


private class ControlsPanelMouseListener extends MouseAdapter {

    private Point mousePressed;

    private Point mouseReleased;

    private ControlsPanelMouseListener () {
        mousePressed = new Point();
        mouseReleased = new Point();
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        mousePressed = e.getPoint();
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
           Shape shape = (Shape) shapeChoice.getSelectedItem();
           mouseReleased = e.getPoint();

           //Get points to correct shape.....

           //Call the shape draw method....
           shape.draw(getGraphics());
        }        
    }

形状抽象类

public abstract class Shape
{
   public abstract void draw(Graphics context);

   public abstract Color getColor();
}

形状层次结构 Shape Hierarchy

1 个答案:

答案 0 :(得分:0)

The benefit of OOP and inheritance is that you can have a base class (or interface) which defines the methods that ALL the sub-classes have BUT the sub-classes will actually perform the logic

Consider

public class Triangle extends Shape {
   ...
   public void draw () {
     System.out.println ("I have three sides");
   }
}

public class Square extends Shape {
   ...
   public void draw () {
     System.out.println ("I have four sides");
   }
}

... main () {

    Shape shapeA = new Triangle ();
    Shape shapeB = new Square ();

    shapeA.draw ();
    shapeB.draw ();

// output

     I have three sides
     I have four sides