Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查找要调用的子类_Java - Fatal编程技术网

Java 查找要调用的子类

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

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

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 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 ();
//输出

     I have three sides
     I have four sides

为什么这个被否决了?为什么你需要知道它是什么形状?好的抽象的要点是你不需要知道这些。关于下一票:文本格式墙,我想没有例子。我真的不需要知道它是哪个形状,但在这个例子中,我需要一些方法来获得shape子类的draw方法的点。这会让我在shape类中创建额外的方法来设置点。困惑:一个实例知道它是哪种类型,你能用一些代码来说明你的意思吗?有时你需要在shape类中添加更多的方法,这样你就可以以一种抽象统一的方式与所有子类交互。所以我真的需要通过在超类中创建与所有子类相关的方法来维护抽象性……是的,如果没有任何通用代码,但是,只有普通的结构,然后考虑使用接口,你会做公共无效设置坐标轴(点起始点,点终结点){//一些代码},或者你会做公共无效设置起始点(点P){}和设置点(点P){ }我的道歉代码中的代码是很难阅读的。这将应用于shape类。基于此代码很难判断Charles。如果您可能需要为每个点使用单独的setter和getter,那么单独的方法将重用现有的代码,否则,由您决定。
     I have three sides
     I have four sides