本文介绍了Java中的内部类和本地内部类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 如果一个类是局部内部类,是否意味着它在另一个类的方法内部,或者它只是在某个地方的另一个方法中定义。If a class is a local inner class, does this mean that it is inside a method of another class or does it mean that it is just defined in another method somewhere.例如,在下面的代码中,MenuListener是否被视为内部本地类?For example, in the code below, is the MenuListener considered an inner local class?import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MenuDemo extends JFrame{private Container c;private ImageIcon[] images = new ImageIcon[5];private JLabel picture;private JPanel mainPanel;private JMenuBar menuBar;private JMenu menu;private JMenuItem bird,cat,dog,rabbit,pig;public MenuDemo() { super("Menu Demo"); c = getContentPane(); c.setLayout(new BorderLayout()); mainPanel = new JPanel(); // Store the animal pictures in an array. for (int i=0; i<5; i++){ images[i] = new ImageIcon("image" + i + ".gif"); } //Set up the picture label. picture = new JLabel(images[0]); picture.setPreferredSize(new Dimension(177,122)); mainPanel.add(picture, BorderLayout.CENTER); c.add(mainPanel); buildMenuBar(); this.setJMenuBar(menuBar); pack(); setVisible(true);}private void buildMenuBar(){ MenuListener listener = new MenuListener(); menuBar = new JMenuBar(); menu = new JMenu("Animals"); menu.setMnemonic(KeyEvent.VK_A); menuBar.add(menu); bird = new JMenuItem("Bird", KeyEvent.VK_B); bird.addActionListener(listener); menu.add(bird); cat = new JMenuItem("Cat", KeyEvent.VK_C); cat.addActionListener(listener); menu.add(cat); dog = new JMenuItem("Dog", KeyEvent.VK_D); dog.addActionListener(listener); menu.add(dog); rabbit = new JMenuItem("Rabbit", KeyEvent.VK_R); rabbit.addActionListener(listener); menu.add(rabbit); pig = new JMenuItem("Pig", KeyEvent.VK_P); pig.addActionListener(listener); menu.add(pig);}private class MenuListener implements ActionListener { public void actionPerformed(ActionEvent e){ if (e.getSource() == bird) picture.setIcon(images[0]); else if (e.getSource() == cat) picture.setIcon(images[1]); else if (e.getSource() == dog) picture.setIcon(images[2]); else if (e.getSource() == rabbit) picture.setIcon(images[3]); if (e.getSource() == pig) picture.setIcon(images[4]); }}public static void main(String[] args){ MenuDemo m = new MenuDemo(); m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}推荐答案 根据一些Oracle Java官方教程,本地内部类是在代码块中定义的类,而不是另一个类的定义的一部分。这意味着在方法内部或初始化程序块中定义的类。According to some official Oracle Java tutorial, a local inner class is a class that is defined in a code block rather than a part of another class' definition. This means a class defined inside a method or in an initializer block.按照该定义, MenuListener 不是本地内部类-只是内部类。Following that definition, MenuListener is not a local inner class -- it is just an inner class.因此:public class Outer { public class Inner1 { } public static class Inner2 { } public void method(){ class LocalInner{ } LocalInner yesThisActuallyCompiles = new LocalInner(); }} 这篇关于Java中的内部类和本地内部类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 22:40