当前位置: 技术问答>java相关
To masterz():本人愚笨,向你再请教一个问题 在静态上下文中不能引用非静态变量 this
来源: 互联网 发布时间:2015-03-09
本文导语: MyExit.java:7: 在静态上下文中不能引用非静态变量 this static protected WindowAdapter e=new MyAdapter(); import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyExit extends JFrame { static protected WindowAdapte...
MyExit.java:7: 在静态上下文中不能引用非静态变量 this
static protected WindowAdapter e=new MyAdapter();
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
static protected WindowAdapter e=new MyAdapter();
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
下面这个程序为什么对
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
protected WindowAdapter e=new MyAdapter();
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
}
public MyExit()
{
setSize(200,200);
my.addWindowListener(e);
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
static protected WindowAdapter e=new MyAdapter();
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
static protected WindowAdapter e=new MyAdapter();
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
下面这个程序为什么对
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
protected WindowAdapter e=new MyAdapter();
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
}
public MyExit()
{
setSize(200,200);
my.addWindowListener(e);
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
static protected WindowAdapter e=new MyExit().new MyAdapter();
public static void main(String[] args)
{
System.out.println("main");
JFrame my=new MyExit();
my.show();
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
class MyAdapter extends WindowAdapter
{
public MyAdapter()
{
super();
System.out.println("MyAdapter()");
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
//output
//MyAdapter()
//main
//说明static protected WindowAdapter e在main之前就会产生,而MyAdapter是内部类,需要由父类来建立它的实例
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
static protected WindowAdapter e=new MyExit().new MyAdapter();
public static void main(String[] args)
{
System.out.println("main");
JFrame my=new MyExit();
my.show();
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
class MyAdapter extends WindowAdapter
{
public MyAdapter()
{
super();
System.out.println("MyAdapter()");
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
//output
//MyAdapter()
//main
//说明static protected WindowAdapter e在main之前就会产生,而MyAdapter是内部类,需要由父类来建立它的实例
|
MyAdapter 是个内部类,静态变量和方法不可以直接引用。
除你的更改方法外
可重新定义MyAdapter
static class MyAdapter extends WindowAdapter
货]
static protected WindowAdapter e=new MyExit().new MyAdapter();
除你的更改方法外
可重新定义MyAdapter
static class MyAdapter extends WindowAdapter
货]
static protected WindowAdapter e=new MyExit().new MyAdapter();
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。