日期:2014-05-20  浏览次数:20773 次

关于静态方法与非静态方法
各位大虾,下面这段程序是核心技术卷图形设计那一章的里的代码,我对NotHelloWorldFrame   类中构造器如此调用setTitle( "NotHelloWorld ")以及
setSize(DEFAULT_WIDTH,   DEFAULT_HEIGHT)感到奇怪,根据我以前掌握的知识似乎只有静态方法才能如此调用,但是我在API里面查询到这两个方法并不是静态方法,而是实例方法,我想应该是   new   JFrame().setTitle( "NotHelloWorld ")    
new   JFrame().setSize(DEFAULT_WIDTH,   DEFAULT_HEIGHT)

import   javax.swing.JFrame;
import   javax.swing.JPanel;
import   java.awt.Graphics;

public   class   NotHelloWorld
{
    public   static   void   main(String[]   args)
    {
        NotHelloWorldFrame   frame   =   new   NotHelloWorldFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

/**
      A   frame   that   contains   a   message   panel
*/
class   NotHelloWorldFrame   extends   JFrame
{
    public   NotHelloWorldFrame()
    {
        setTitle( "NotHelloWorld ");
        setSize(DEFAULT_WIDTH,   DEFAULT_HEIGHT);

        //   add   panel   to   frame
        NotHelloWorldPanel   panel   =   new   NotHelloWorldPanel();
        add(panel);
    }

    public   static   final   int   DEFAULT_WIDTH   =   300;
    public   static   final   int   DEFAULT_HEIGHT   =   200;
}

class   NotHelloWorldPanel   extends   JPanel
{
    public   void   paintComponent(Graphics   g)
    {
        super.paintComponent(g);
        g.drawString( "Not   a   Hello,   World   program ",   MESSAGE_X,   MESSAGE_Y);
    }

    public   static   final   int   MESSAGE_X   =   75;
    public   static   final   int   MESSAGE_Y   =   100;
}

------解决方案--------------------
super在这可省略,因为子类并未重写父类此方法。