抽象类一般是用来被继承的,但是不想继承直接用可以吗(可能因为要重写很多方法)?把它弄成一个静态的东西。最近看到一个用法:要用抽象类Graphics中的方法,但是它的子类有DebugGraphics和Graphics2D共两个,Graphics2D也是抽象类,也没有见它使用这两个子类,就直接调用Graphics中的方法,我发现都加了一个static。例如:
public class MyClass {
  public static Graphics g;
  public static int[] x;
  public static int[] y;
  public static int n;  public static void main(String[] args){
    g.drawPolyline(x, y, n);
    // ......    }
} 抽象类可以这样用吗?还有这样的用途吗?

解决方案 »

  1.   

    抽象类可以有实现的方法,但不能实例化。加static可以编译通过,但调用会报错
    不要这样用
      

  2.   

    我把它声明为static的,只重写我要的方法,其他的不管:
    public class MyClass {
      public static Graphics g;
      public static int[] x;
      public static int[] y;
      public static int n;  public static void main(String[] args){
        g.drawPolyline(x, y, n){
        .......
        }
      ......     }
    }
    我看到好像有人就是这样用的,可以运行。
      

  3.   

    为什么不可以呢……
    实际运行的情况这个g应该被实例化,而且是一个Graphics的具体子类就像平时写的List<T> list;到实际使用时会被实例化比如list = new ArrayList<T>();我们也照样拿着list用来用去
      

  4.   

    这就是所谓的多态吧。但是我看到的这个用法他并没有传Graphics的子类,也没有自己写一个Graphics子类,然后传进去。我看到的代码有点老jdk1.3或1.4,是不是老版本的Graphics不是抽象类?
      

  5.   

    Graphics/Graphics2D 一直是抽象类、由 jre提供底层实现类、在 awt/swing/java2d 程序运行时初始化。
      

  6.   

    我把代码放在这里大家帮我分析分析,O(∩_∩)O谢谢!后面的文字是注释和我的问题。package numbercruncher.graphutils; //自己定义的两个包numbercruncher,graphutilsimport java.awt.*;
    import java.awt.event.*;/**
     * The panel that draws a set of axes, and plots points and lines.
     */
    class PlotPanel extends Panel
    {
        private static final int TICK_SIZE = 5;    /** image buffer */             private Image       buffer;
        /** buffer graphics context */  private Graphics    bg;
        /** font metrics */             private FontMetrics fontMetrics;    /** label font */   private Font labelFont = new Font("Dialog", 1, 10);    /** width */                private int   w;
        /** height */               private int   h;
        /** x-axis row */           private int   xAxisRow;
        /** y-axis column */        private int   yAxisCol;
        /** minimum x value */      private float xMin;
        /** maximum x value */      private float xMax;
        /** minimum y value */      private float yMin;
        /** maximum y value */      private float yMax;
        /** x delta per pixel */    private float xDelta;
        /** y delta per pixel */    private float yDelta;    /** array of plot endpoint columns */   private int cs[];
        /** array of plot endpoint rows */      private int rs[];
        /** endpoint array index */             private int k;    /** parent graph panel */   private GraphPanel graphPanel; //GraphPanel是自己定义的一个类    /**
         * Constructor.
         * @param graphPanel the parent graph panel
         */
        PlotPanel(GraphPanel graphPanel)  
        {
            this.graphPanel = graphPanel;
            setBackground(Color.white);   //setBackground方法是Component(抽象类)里边的,这里应该是算Panel(Component的子类)的吧?               // Plot mouse event handlers.
            addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent ev)
                {
                    PlotPanel.this.graphPanel.mouseClickedOnPlot(ev);   // callback
                }            public void mousePressed(MouseEvent ev)
                {
                    PlotPanel.this.graphPanel.mousePressedOnPlot(ev);   // callback
                }            public void mouseReleased(MouseEvent ev)
                {
                    PlotPanel.this.graphPanel.mouseReleasedOnPlot(ev);  // callback
                }
            });
            addMouseMotionListener(new MouseMotionAdapter()
            {
                public void mouseDragged(MouseEvent ev)
                {
                    PlotPanel.this.graphPanel.mouseDraggedOnPlot(ev);   // callback
                }
            });   //内部类
        }    /**
         * Initialize the plot with its properties.
         * @param the plot properties
         */
        void initPlot(PlotProperties plotProps) //PlotProperties是自己定义的一个类
        {
            if (plotProps == null) return;        // Compute the plot properties.
            Dimension size = getSize();
            plotProps.compute(size);        // Extract the plot properties.
            w        = plotProps.getWidth();
            h        = plotProps.getHeight();
            xAxisRow = plotProps.getXAxisRow();
            yAxisCol = plotProps.getYAxisColumn();
            xMin     = plotProps.getXMin();
            xMax     = plotProps.getXMax();
            yMin     = plotProps.getYMin();
            yMax     = plotProps.getYMax();
            xDelta   = plotProps.getXDelta();
            yDelta   = plotProps.getYDelta();        // Create the image buffer and get its graphics context.
            buffer = createImage(w, h);    //createImage方法是Component(抽象类)里边的,这里也应该是算Panel(子类)的吧?
            bg     = buffer.getGraphics(); //createImage方法返回一个Image(抽象类)(Image buffer;),怎么就可以直接用呢?        bg.setFont(labelFont);
            fontMetrics = bg.getFontMetrics();
        }    .........
    }
      

  7.   

    我发现Image类(抽象类)和Component类(抽象类)中都有getGraphics()方法,buffer.getGraphics()方法是调用了哪个的啊?我想是不是算调用了Component的子类Panel的?因为这个类是Panel的子类。