主题:[讨论]内部类访问局部变量为什么要声明为final
Java中匿名内部类访问局部变量为什么要声明为final类型啊,可为什么在main()方法中使用匿名内部类与在其他方法中会有不同?
请各位大虾看看下面两个程序:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Logo
{
private String name;
private String pass;
public Logo(String name,String pass)
{
this.name=name;
this.pass=pass;
}
public boolean fun(String name,String pass)
{
if((this.name.equals("084221019"))&&(this.pass.equals("t1990520123")))return true;
else return false;
}
}
public class JFrameDemo08
{
public static void main(String args[])throws Exception
{
JFrame frame=new JFrame("this is a windows");
JLabel label1=new JLabel("账号");
JTextField text=new JTextField(10);
JButton but1=new JButton("登陆");
JLabel label2=new JLabel("密码");
JPasswordField password =new JPasswordField(10);
JButton but2=new JButton("重置");
JLabel show=new JLabel("请登录");
frame.setLayout(new FlowLayout());
frame.add(label1);
frame.add(text);
frame.add(but1);
frame.add(label2);
frame.add(password);
frame.add(but2);
frame.add(show);
password.setEchoChar('#');
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but1)
{
String textname=text.getText();
String passname=new String(password.getPassword());
Logo logo=new Logo(textname,passname);
if(logo.fun(textname,passname))
{
show.setText("登陆成功");
}
else
{
show.setText("账号或密码错误!!!请重新输入");
}
}
}
});
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but2)
{
text.setText("");
password.setText("");
show.setText("请重新输入账号和密码!!!!");
}
}
});
frame.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
System.out.println("窗口已经关闭");
System.exit(1);
}
public void windowClosed(WindowEvent e)
{
System.out.println("窗口被关闭");
}
public void windowIconified(WindowEvent e)
{
System.out.println("窗口最小化");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("窗口从最小化到正常");
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
});
frame.pack();
frame.setVisible(true);
}
}
上面这个运行会出错,但下面这个就没问题:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Logo
{
private String name;
private String pass;
public Logo(String name,String pass)
{
this.name=name;
this.pass=pass;
}
public boolean fun(String name,String pass)
{
if((this.name.equals("084221019"))&&(this.pass.equals("t1990520123")))return true;
else return false;
}
}
class Performer
{
private JFrame frame=new JFrame("this is a windows");
private JLabel label1=new JLabel("账号");
private JTextField text=new JTextField(10);
private JButton but1=new JButton("登陆");
private JLabel label2=new JLabel("密码");
private JPasswordField password =new JPasswordField(10);
private JButton but2=new JButton("重置");
private JLabel show=new JLabel("请登录");
public Performer()
{
frame.setLayout(new FlowLayout());
frame.add(label1);
frame.add(text);
frame.add(but1);
frame.add(label2);
frame.add(password);
frame.add(but2);
frame.add(show);
password.setEchoChar('#');
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but1)
{
String textname=text.getText();
String passname=new String(password.getPassword());
Logo logo=new Logo(textname,passname);
if(logo.fun(textname,passname))
{
show.setText("登陆成功");
}
else
{
show.setText("账号或密码错误!!!请重新输入");
}
}
}
});
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but2)
{
text.setText("");
password.setText("");
show.setText("请重新输入账号和密码!!!!");
}
}
});
frame.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
System.out.println("窗口已经关闭");
System.exit(1);
}
public void windowClosed(WindowEvent e)
{
System.out.println("窗口被关闭");
}
public void windowIconified(WindowEvent e)
{
System.out.println("窗口最小化");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("窗口从最小化到正常");
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
});
frame.pack();
frame.setVisible(true);
}
}
public class JFrameDemo08
{
public static void main(String args[])throws Exception
{
new Performer();
}
}
请各位大虾看看下面两个程序:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Logo
{
private String name;
private String pass;
public Logo(String name,String pass)
{
this.name=name;
this.pass=pass;
}
public boolean fun(String name,String pass)
{
if((this.name.equals("084221019"))&&(this.pass.equals("t1990520123")))return true;
else return false;
}
}
public class JFrameDemo08
{
public static void main(String args[])throws Exception
{
JFrame frame=new JFrame("this is a windows");
JLabel label1=new JLabel("账号");
JTextField text=new JTextField(10);
JButton but1=new JButton("登陆");
JLabel label2=new JLabel("密码");
JPasswordField password =new JPasswordField(10);
JButton but2=new JButton("重置");
JLabel show=new JLabel("请登录");
frame.setLayout(new FlowLayout());
frame.add(label1);
frame.add(text);
frame.add(but1);
frame.add(label2);
frame.add(password);
frame.add(but2);
frame.add(show);
password.setEchoChar('#');
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but1)
{
String textname=text.getText();
String passname=new String(password.getPassword());
Logo logo=new Logo(textname,passname);
if(logo.fun(textname,passname))
{
show.setText("登陆成功");
}
else
{
show.setText("账号或密码错误!!!请重新输入");
}
}
}
});
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but2)
{
text.setText("");
password.setText("");
show.setText("请重新输入账号和密码!!!!");
}
}
});
frame.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
System.out.println("窗口已经关闭");
System.exit(1);
}
public void windowClosed(WindowEvent e)
{
System.out.println("窗口被关闭");
}
public void windowIconified(WindowEvent e)
{
System.out.println("窗口最小化");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("窗口从最小化到正常");
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
});
frame.pack();
frame.setVisible(true);
}
}
上面这个运行会出错,但下面这个就没问题:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Logo
{
private String name;
private String pass;
public Logo(String name,String pass)
{
this.name=name;
this.pass=pass;
}
public boolean fun(String name,String pass)
{
if((this.name.equals("084221019"))&&(this.pass.equals("t1990520123")))return true;
else return false;
}
}
class Performer
{
private JFrame frame=new JFrame("this is a windows");
private JLabel label1=new JLabel("账号");
private JTextField text=new JTextField(10);
private JButton but1=new JButton("登陆");
private JLabel label2=new JLabel("密码");
private JPasswordField password =new JPasswordField(10);
private JButton but2=new JButton("重置");
private JLabel show=new JLabel("请登录");
public Performer()
{
frame.setLayout(new FlowLayout());
frame.add(label1);
frame.add(text);
frame.add(but1);
frame.add(label2);
frame.add(password);
frame.add(but2);
frame.add(show);
password.setEchoChar('#');
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but1)
{
String textname=text.getText();
String passname=new String(password.getPassword());
Logo logo=new Logo(textname,passname);
if(logo.fun(textname,passname))
{
show.setText("登陆成功");
}
else
{
show.setText("账号或密码错误!!!请重新输入");
}
}
}
});
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but2)
{
text.setText("");
password.setText("");
show.setText("请重新输入账号和密码!!!!");
}
}
});
frame.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
System.out.println("窗口已经关闭");
System.exit(1);
}
public void windowClosed(WindowEvent e)
{
System.out.println("窗口被关闭");
}
public void windowIconified(WindowEvent e)
{
System.out.println("窗口最小化");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("窗口从最小化到正常");
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
});
frame.pack();
frame.setVisible(true);
}
}
public class JFrameDemo08
{
public static void main(String args[])throws Exception
{
new Performer();
}
}