【问题标题】:The difference of static and non-static inner classes?静态和非静态内部类的区别?
【发布时间】:2013-07-05 01:57:19
【问题描述】:

我正在阅读 Effective 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");
        }
    }
}

输出是

我是内在的 我很内向

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

我想知道为什么在这个例子中使用非静态类?

【问题讨论】:

    标签: java oop inner-classes nested-class


    【解决方案1】:
    In the case of creating instance, the instance of non s
    static inner class is created with the reference of
    object of outer class in which it is defined……this
    means it have inclosing instance …….
    But the instance of static inner class
    is created with the reference of Outer class, not with
    the reference of object of outer class…..this means it
    have not inclosing instance…
    For example……
    class A
    {
    class B
    {
    // static int x; not allowed here…..
    
    }
    static class C
    {
    static int x; // allowed here
    }
    }
    
    class Test
    {
    public static void main(String… str)
    {
    A o=new A();
    A.B obj1 =o.new B();//need of inclosing instance
    
    A.C obj2 =new A.C();
    
    // not need of reference of object of outer class….
    }
    }
    

    【讨论】:

      【解决方案2】:

      没有静态内部类,它是静态嵌套类。 “非静态 [嵌套] 类的每个实例都与其包含类的封闭实例隐式关联......可以在封闭实例上调用方法。”

      静态嵌套类无权访问封闭实例。

      参考:this so thread

      【讨论】:

        【解决方案3】:

        不同之处在于非静态内部类有一个对包含类的隐式引用。

        public class JavaApplication7 {
        
          //You can access this attribute in non-static inner class
          private String anyAttribute;
        
          public Inner getAClass(){
            return new Inner();
          }
        
          static class Sinner{
            public void testIt(){
              //Here, you cannot access JavaApplication7.this
            }
          }
        
          class Inner{
            public void testIt(){
                //Here, you can access JavaApplication7.this
                //You can also access *anyAttribute* or call non-static method getAClass()
            }
          }
        }
        

        【讨论】:

          【解决方案4】:

          static 和非静态嵌套类之间的区别在于,非静态嵌套类与外部类的实例隐式关联,它们可以引用为OuterClassName.this。在实现迭代器时,此引用很有用,因为它们需要访问与其相关的集合的成员。您可以通过使用 static 嵌套类来实现相同的目的,该嵌套类显式传递了对外部类的引用。

          【讨论】:

            【解决方案5】:

            迭代器通常首先需要引用用于创建它的集合。您可以使用一个静态嵌套类来执行此操作,该类显式提供对集合的引用 - 或者您可以只使用一个内部类,它隐式具有该引用。

            基本上,如果嵌套类的每个实例都需要一个封闭类的实例来操作(并且该实例不会改变),那么您不妨将其设为内部类。否则,将其设为静态嵌套类。

            【讨论】:

              猜你喜欢
              • 2016-08-20
              • 1970-01-01
              • 2012-05-26
              • 2011-07-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-08-29
              相关资源
              最近更新 更多