不要说是"规则"什么的,我想知道原理,谢谢!

解决方案 »

  1.   

    我可以作一个反面的假设来理解.
    假设 它所引用的外部对象可以不是 final 的
    也就是说 内部类 可以对这个对象进行任意修改.
    如果这个传入的对象还在其他地方被引用
    这种修改对它们来说是透明的.所以回出现一些意想不到的结果.
    所以出于这个原因. sun 要把它设计为 final  的把.
      

  2.   

    我在学习这一块的时候也不太理解为什么内部类在调用外围类的变量里,外围类的变量必须为final期待达人解惑
      

  3.   

    别死记!~
    import java.awt.*;import javax.swing.*;
    import java.awt.event.*;public class SwingApplication {
        
        private static int numClicks=0;
        //private static JLabel l;
        public static void main(String args[]) {
            JFrame f = new JFrame("Java10-[C.R.S.M]K01");
            JButton b = new JButton("Swing Button");
            final JLabel l = new JLabel("You have click: " + "0 " + "次");
            JPanel p = new JPanel();
            b.addActionListener(new ButtonHandler());
            p.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
            p.setLayout(new GridLayout(0, 1));
            f.add(p);
            p.add(b);
            p.add(l);
            f.setSize(200, 200);
            f.setVisible(true);
        }    static class ButtonHandler implements ActionListener { // 加上 public 也不对。
            public void actionPerformed(ActionEvent e) {            numClicks++;
                l.setText("You have click: " + numClicks + "次");// 输出l后面那个点时没有弹出对应的方法。
            }
        }
    }这个程序是有问题的!~
    改成这样就对了:
    import java.awt.*;import javax.swing.*;
    import java.awt.event.*;public class SwingApplication {
        
        private static int numClicks=0;
        private static JLabel l;
        public static void main(String args[]) {
            JFrame f = new JFrame("Java10-[C.R.S.M]K01");
            JButton b = new JButton("Swing Button");
            l = new JLabel("You have click: " + "0 " + "次");
            JPanel p = new JPanel();
            b.addActionListener(new ButtonHandler());
            p.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
            p.setLayout(new GridLayout(0, 1));
            f.add(p);
            p.add(b);
            p.add(l);
            f.setSize(200, 200);
            f.setVisible(true);
        }    static class ButtonHandler implements ActionListener { 
            public void actionPerformed(ActionEvent e) {            numClicks++;
                l.setText("You have click: " + numClicks + "次");
            }
        }
    }呵呵,这是我今天的问题。
    帖子地址:http://topic.csdn.net/u/20080413/11/febf2907-031c-427d-98d1-412978c4cc97.html?seed=926383211