抽象类一般是用来被继承的,但是不想继承直接用可以吗(可能因为要重写很多方法)?把它弄成一个静态的东西。最近看到一个用法:要用抽象类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);
// ...... }
} 抽象类可以这样用吗?还有这样的用途吗?
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);
// ...... }
} 抽象类可以这样用吗?还有这样的用途吗?
不要这样用
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){
.......
}
...... }
}
我看到好像有人就是这样用的,可以运行。
实际运行的情况这个g应该被实例化,而且是一个Graphics的具体子类就像平时写的List<T> list;到实际使用时会被实例化比如list = new ArrayList<T>();我们也照样拿着list用来用去
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();
} .........
}