简体   繁体   English

java中的内部类和本地内部类之间有什么区别?

[英]what is the difference between an inner class and a local inner class in 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. 如果一个类是一个本地内部类,这是否意味着它在另一个类的方法中,或者它是否意味着它只是在某个地方的另一个方法中定义。

For example, in the code below, is the MenuListener considered an inner local class? 例如,在下面的代码中,MenuListener是否被视为内部本地类?

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);
}   
}

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. 根据一些官方的Oracle Java教程 ,本地内部类是在代码块中定义的类,而不是另一个类定义的一部分。 This means a class defined inside a method or in an initializer block. 这意味着在方法或初始化程序块中定义的类。

Following that definition, MenuListener is not a local inner class -- it is just an inner class. 遵循该定义, MenuListener不是本地内部类 - 它只是一个内部类。

So: 所以:

public class Outer {

    public class Inner1 {
    }

    public static class Inner2 {
    }

    public void method(){

        class LocalInner{
        }

        LocalInner yesThisActuallyCompiles = new LocalInner();
    }
}

If a class is a local inner class, does this mean that it is inside a method 如果一个类是本地内部类,这是否意味着它在一个方法中

Yes. 是。

of another class 另一堂课

No. Of a class, but any method has to be in a class. 号的类,但任何方法必须是在一个类。

or does it mean that it is just defined in another method somewhere. 或者它是否意味着它只是在某个地方用另一种方法定义的。

Yes. 是。

There not much difference between these, except for the difference between 'in another class' and 'somewhere'. 这些之间没有太大区别,除了“在另一个班级”和“某个地方”之间的区别。 It's not really clear what you're actually asking. 你真正要问的是什么并不是很清楚。

Inner Class/ Member Class 内班/会员班

A non-static class that is created inside a class but outside a method is called member inner class. 在类中但在方法外部创建的非静态类称为成员内部类。

class TestMemberOuter1{  
 private int data=30;  
 class Inner{  
  void msg(){System.out.println("data is "+data);}  
 }  
 public static void main(String args[]){  
  TestMemberOuter1 obj=new TestMemberOuter1();  
  TestMemberOuter1.Inner in=obj.new Inner();  
  in.msg();  
 }  
}

Local Inner Class 本地内部班级

A class ie created inside a method is called local inner class in java. 在方法内创建的类在java中称为本地内部类。 If you want to invoke the methods of local inner class, you must instantiate this class inside the method 如果要调用本地内部类的方法,则必须在方法内实例化此类

   public class localInner1{  
     private int data=30; 
     void display(){  
      class Local{  
       void msg(){System.out.println(data);}  
      }  
      Local l=new Local();  
      l.msg();  
     }  
     public static void main(String args[]){  
      localInner1 obj=new localInner1();  
      obj.display();  
     }  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM