静态和非静态内部类的区别? [英] The difference of static and non-static inner classes?

查看:291
本文介绍了静态和非静态内部类的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有效的Java 2-Item 22,并在标题中写明:

非静态成员偏爱静态成员"

但在本章结尾

集合接口(例如Set和List)的实现, 通常使用非静态成员类来实现其迭代器:

// Typical use of a nonstatic member class
public class MySet<E> extends AbstractSet<E> {
    ... // Bulk of the class omitted

    public Iterator<E> iterator() {
        return new MyIterator();
    }

    private class MyIterator implements Iterator<E> {
        ...
    }
}

我制作了一个测试程序,以查看它们之间是否有任何区别.

public class JavaApplication7 {


    public static void main(String[] args) {
        // TODO code application logic here
        JavaApplication7 t = new JavaApplication7();

        Inner nonStaticObject = t.getAClass();
        Sinner staticObject = new JavaApplication7.Sinner();

        nonStaticObject.testIt();
        staticObject.testIt();         
    }

    public Inner getAClass(){
        return new Inner();
    }

    static class Sinner{
        public void testIt(){
            System.out.println("I am inner");
        }
    }

    class Inner{
        public void testIt(){
            System.out.println("I am inner");
        }
    }
}

输出为

我内在 我内心

所以,他们做了同样的工作.

我想知道为什么在此示例中使用非静态类吗?

解决方案

迭代器通常首先需要引用用于创建它的集合.您可以使用一个静态的嵌套类来做到这一点,该类显式地提供了对集合的引用-或者您可以只使用一个内部类,该类隐式地具有该引用.

基本上,如果嵌套类的每个实例都需要封闭类的一个实例进行操作(并且该实例不变),那么您也可以将其设为内部类.否则,将其设置为静态嵌套类.

I was reading Effective Java 2 - Item 22 and it says in the title:

"Favor static member classes over non-static"

but at the end of the chapter

Implementations of the collection interfaces, such as Set and List, typically use nonstatic member classes to implement their iterators:

// Typical use of a nonstatic member class
public class MySet<E> extends AbstractSet<E> {
    ... // Bulk of the class omitted

    public Iterator<E> iterator() {
        return new MyIterator();
    }

    private class MyIterator implements Iterator<E> {
        ...
    }
}

I made a test program to see if there is any difference between them and here it is.

public class JavaApplication7 {


    public static void main(String[] args) {
        // TODO code application logic here
        JavaApplication7 t = new JavaApplication7();

        Inner nonStaticObject = t.getAClass();
        Sinner staticObject = new JavaApplication7.Sinner();

        nonStaticObject.testIt();
        staticObject.testIt();         
    }

    public Inner getAClass(){
        return new Inner();
    }

    static class Sinner{
        public void testIt(){
            System.out.println("I am inner");
        }
    }

    class Inner{
        public void testIt(){
            System.out.println("I am inner");
        }
    }
}

The output is

I am inner I am inner

So, they did the same job.

I wonder Why non-static class is used in this example?

解决方案

An iterator usually needs to refer to the collection used to create it in the first place. You can do that with a static nested class which is explicitly provided with a reference to the collection - or you can just use an inner class, which has that reference implicitly.

Basically, if every instance of the nested class needs an instance of the enclosing class to operate (and that instance doesn't change), then you might as well make it an inner class. Otherwise, make it a static nested class.

这篇关于静态和非静态内部类的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆