问题描述
importjava.awt.*;importjavax.swing.*;publicclassMyPanelextendsJPanel{publicvoidpaintComponent(Graphicsg){g.drawLine(15,15,150,150);}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubJFrameframe=newJFrame();MyPanelpanel=newMyPanel();frame.add(panel);frame.setVisible(true);frame.setSize(500,400);}}
API中查阅DrawLine方法如下图:上图也看到了这个方法是抽象的,于是我查阅了java中DrawLine的源码publicabstractvoiddrawLine(intx1,inty1,intx2,inty2);/***Fillsthespecifiedrectangle.*Theleftandrightedgesoftherectangleareat*<code>x</code>and<code>x + width - 1</code>.*Thetopandbottomedgesareat*<code>y</code>and<code>y + height - 1</code>.*Theresultingrectanglecoversanarea*<code>width</code>pixelswideby*<code>height</code>pixelstall.*Therectangleisfilledusingthegraphicscontext'scurrentcolor.*@paramxthe<i>x</i>coordinate*oftherectangletobefilled.*@paramythe<i>y</i>coordinate*oftherectangletobefilled.*@paramwidththewidthoftherectangletobefilled.*@paramheighttheheightoftherectangletobefilled.*@seejava.awt.Graphics#clearRect*@seejava.awt.Graphics#drawRect*/
以上是DrawLine的源码,也没有方法主体。而我在用DrawLine的时候没有重写这个方法,为什么可以使用呢?